Skip to content

Commit c6511fd

Browse files
[MSHARED-1009] Allow providing Maven executable from workspace
1 parent 7e3c647 commit c6511fd

File tree

5 files changed

+133
-33
lines changed

5 files changed

+133
-33
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ under the License.
106106
<detectLinks>false</detectLinks>
107107
</configuration>
108108
</plugin>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-resources-plugin</artifactId>
112+
<version>3.2.0</version>
113+
</plugin>
109114
<plugin>
110115
<artifactId>maven-site-plugin</artifactId>
111116
<version>3.10.0</version>

src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,17 @@ public Commandline build( InvocationRequest request )
6262
throws CommandLineConfigurationException
6363
{
6464

65+
Commandline cli = new Commandline();
66+
6567
setupMavenHome( request );
66-
checkRequiredState();
6768

68-
try
69-
{
70-
setupMavenExecutable( request );
71-
}
72-
catch ( IOException e )
73-
{
74-
throw new CommandLineConfigurationException( e.getMessage(), e );
75-
}
69+
// discover value for working directory
70+
setupBaseDirectory( request );
71+
cli.setWorkingDirectory( baseDirectory );
7672

77-
Commandline cli = new Commandline();
73+
checkRequiredState();
7874

75+
setupMavenExecutable( request );
7976
cli.setExecutable( mavenExecutable.getAbsolutePath() );
8077

8178
// handling for OS-level envars
@@ -89,11 +86,6 @@ public Commandline build( InvocationRequest request )
8986
// includes/excludes, etc.
9087
setReactorBehavior( request, cli );
9188

92-
// discover value for working directory
93-
setupBaseDirectory( request );
94-
95-
cli.setWorkingDirectory( baseDirectory );
96-
9789
// local repository location
9890
setLocalRepository( request, cli );
9991

@@ -591,10 +583,9 @@ else if ( System.getProperty( "maven.home" ) != null )
591583
*
592584
* @param request a Invoker request
593585
* @throws org.apache.maven.shared.invoker.CommandLineConfigurationException if any.
594-
* @throws java.io.IOException if any.
595586
*/
596587
protected void setupMavenExecutable( InvocationRequest request )
597-
throws CommandLineConfigurationException, IOException
588+
throws CommandLineConfigurationException
598589
{
599590
if ( request.getMavenExecutable() != null )
600591
{
@@ -608,38 +599,63 @@ protected void setupMavenExecutable( InvocationRequest request )
608599
{
609600
executable = mavenExecutable.getPath();
610601
}
611-
else if ( Os.isFamily( "windows" ) )
602+
else
603+
{
604+
executable = "mvn";
605+
}
606+
607+
// firs look in project directory
608+
mavenExecutable = detectMavenExecutablePerOs( baseDirectory, executable );
609+
if ( mavenExecutable == null )
612610
{
613-
if ( new File( mavenHome, "/bin/mvn.cmd" ).exists() )
611+
// next maven home
612+
mavenExecutable = detectMavenExecutablePerOs( mavenHome, "/bin/" + executable );
613+
}
614+
615+
if ( mavenExecutable != null )
616+
{
617+
try
614618
{
615-
executable = "mvn.cmd";
619+
mavenExecutable = mavenExecutable.getCanonicalFile();
616620
}
617-
else
621+
catch ( IOException e )
618622
{
619-
executable = "mvn.bat";
623+
logger.debug( "Failed to canonicalize maven executable: '" + mavenExecutable
624+
+ "'. Using as-is.", e );
620625
}
621626
}
622627
else
623628
{
624-
executable = "mvn";
629+
throw new CommandLineConfigurationException(
630+
"Maven executable: '" + executable + "'"
631+
+ " not found at project dir: '" + baseDirectory + "' nor maven home: '" + mavenHome + "'" );
625632
}
633+
}
634+
}
626635

627-
mavenExecutable = new File( mavenHome, "/bin/" + executable );
628-
629-
try
630-
{
631-
mavenExecutable = mavenExecutable.getCanonicalFile();
632-
}
633-
catch ( IOException e )
636+
private File detectMavenExecutablePerOs( File baseDirectory, String executable )
637+
{
638+
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
639+
{
640+
File executableFile = new File( baseDirectory, executable + ".cmd" );
641+
if ( executableFile.isFile() )
634642
{
635-
logger.debug( "Failed to canonicalize maven executable: " + mavenExecutable + ". Using as-is.", e );
643+
return executableFile;
636644
}
637645

638-
if ( !mavenExecutable.isFile() )
646+
executableFile = new File( baseDirectory, executable + ".bat" );
647+
if ( executableFile.isFile() )
639648
{
640-
throw new CommandLineConfigurationException( "Maven executable not found at: " + mavenExecutable );
649+
return executableFile;
641650
}
642651
}
652+
653+
File executableFile = new File( baseDirectory, executable );
654+
if ( executableFile.isFile() )
655+
{
656+
return executableFile;
657+
}
658+
return null;
643659
}
644660

645661
/**

src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.Properties;
3030

31+
import org.apache.maven.shared.utils.Os;
3132
import org.apache.maven.shared.utils.StringUtils;
3233
import org.junit.Test;
3334

@@ -239,6 +240,42 @@ public void testPomOutsideProject() throws Exception
239240
assertEquals( 0, result.getExitCode() );
240241
}
241242

243+
@Test
244+
public void testMavenWrapperInProject() throws Exception
245+
{
246+
File basedir = getBasedirForBuild();
247+
248+
Invoker invoker = newInvoker();
249+
250+
InvocationRequest request = new DefaultInvocationRequest();
251+
request.setBaseDirectory( basedir );
252+
request.setGoals( Collections.singletonList( "test-wrapper-goal" ) );
253+
request.setMavenExecutable( new File( "./mvnw" ) );
254+
255+
final StringBuilder outlines = new StringBuilder();
256+
request.setOutputHandler( new InvocationOutputHandler()
257+
{
258+
@Override
259+
public void consumeLine( String line )
260+
{
261+
outlines.append( line );
262+
}
263+
} );
264+
265+
266+
InvocationResult result = invoker.execute( request );
267+
268+
assertEquals( 0, result.getExitCode() );
269+
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
270+
{
271+
assertEquals( "Windows Wrapper executed", outlines.toString() );
272+
}
273+
else
274+
{
275+
assertEquals( "Unix Wrapper executed", outlines.toString() );
276+
}
277+
}
278+
242279
private Invoker newInvoker()
243280
{
244281
Invoker invoker = new DefaultInvoker();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
# ----------------------------------------------------------------------------
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
# ----------------------------------------------------------------------------
20+
21+
echo "Unix Wrapper executed"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@REM ----------------------------------------------------------------------------
2+
@REM Licensed to the Apache Software Foundation (ASF) under one
3+
@REM or more contributor license agreements. See the NOTICE file
4+
@REM distributed with this work for additional information
5+
@REM regarding copyright ownership. The ASF licenses this file
6+
@REM to you under the Apache License, Version 2.0 (the
7+
@REM "License"); you may not use this file except in compliance
8+
@REM with the License. You may obtain a copy of the License at
9+
@REM
10+
@REM http://www.apache.org/licenses/LICENSE-2.0
11+
@REM
12+
@REM Unless required by applicable law or agreed to in writing,
13+
@REM software distributed under the License is distributed on an
14+
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
@REM KIND, either express or implied. See the License for the
16+
@REM specific language governing permissions and limitations
17+
@REM under the License.
18+
@REM ----------------------------------------------------------------------------
19+
@ECHO off
20+
21+
echo Windows Wrapper executed

0 commit comments

Comments
 (0)