Skip to content

Commit a079ba9

Browse files
[MINVOKER-297] NPE when non-existing Maven Home
1 parent 4d0d70c commit a079ba9

File tree

4 files changed

+89
-41
lines changed

4 files changed

+89
-41
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ under the License.
343343
<artifactId>maven-invoker-plugin</artifactId>
344344
<version>3.2.2</version>
345345
</plugin>
346+
<plugin>
347+
<artifactId>maven-surefire-plugin</artifactId>
348+
<configuration>
349+
<systemPropertyVariables>
350+
<maven.home>${maven.home}</maven.home>
351+
</systemPropertyVariables>
352+
</configuration>
353+
</plugin>
346354
</plugins>
347355
</pluginManagement>
348356

src/main/java/org/apache/maven/plugins/invoker/AbstractInvokerMojo.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ public void execute()
761761
return;
762762
}
763763

764+
setupActualMavenVersion();
765+
764766
handleScriptRunnerWithScriptClassPath();
765767

766768
Collection<String> collectedProjects = new LinkedHashSet<>();
@@ -830,6 +832,25 @@ else if ( lastModifiedRecursive( projectsDirectory ) <= lastModifiedRecursive( c
830832

831833
}
832834

835+
private void setupActualMavenVersion() throws MojoExecutionException
836+
{
837+
if ( mavenHome != null )
838+
{
839+
try
840+
{
841+
actualMavenVersion = SelectorUtils.getMavenVersion( mavenHome );
842+
}
843+
catch ( IOException e )
844+
{
845+
throw new MojoExecutionException( e.getMessage(), e );
846+
}
847+
}
848+
else
849+
{
850+
actualMavenVersion = SelectorUtils.getMavenVersion();
851+
}
852+
}
853+
833854
/**
834855
* Find the latest lastModified recursively within a directory structure.
835856
*
@@ -912,6 +933,7 @@ private void handleScriptRunnerWithScriptClassPath()
912933
scriptRunner = new ScriptRunner( );
913934
scriptRunner.setScriptEncoding( encoding );
914935
scriptRunner.setGlobalVariable( "localRepositoryPath", localRepositoryPath );
936+
scriptRunner.setGlobalVariable( "mavenVersion", actualMavenVersion );
915937
if ( scriptVariables != null )
916938
{
917939
scriptVariables.forEach( ( key, value ) -> scriptRunner.setGlobalVariable( key, value ) );
@@ -1267,16 +1289,6 @@ private void runBuilds( final File projectsDir, List<BuildJob> buildJobs, int ru
12671289

12681290
final File mergedSettingsFile = mergeSettings( interpolatedSettingsFile );
12691291

1270-
if ( mavenHome != null )
1271-
{
1272-
actualMavenVersion = SelectorUtils.getMavenVersion( mavenHome );
1273-
}
1274-
else
1275-
{
1276-
actualMavenVersion = SelectorUtils.getMavenVersion();
1277-
}
1278-
scriptRunner.setGlobalVariable( "mavenVersion", actualMavenVersion );
1279-
12801292
final CharSequence actualJreVersion;
12811293
// @todo if ( javaVersions ) ... to be picked up from toolchains
12821294
if ( javaHome != null )

src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*/
2121

2222
import java.io.File;
23+
import java.io.FileNotFoundException;
2324
import java.io.IOException;
25+
import java.io.InputStream;
2426
import java.net.MalformedURLException;
2527
import java.net.URL;
2628
import java.util.ArrayList;
@@ -109,7 +111,8 @@ static String getMavenVersion()
109111
// if this ever changes, we will have to revisit this code.
110112
Properties properties = new Properties();
111113
// CHECKSTYLE_OFF: LineLength
112-
properties.load( MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
114+
properties.load( MavenProject.class.getClassLoader()
115+
.getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
113116
// CHECKSTYLE_ON: LineLength
114117
return StringUtils.trim( properties.getProperty( "version" ) );
115118
}
@@ -119,33 +122,35 @@ static String getMavenVersion()
119122
}
120123
}
121124

122-
static String getMavenVersion( File mavenHome )
125+
static String getMavenVersion( File mavenHome ) throws IOException
123126
{
124127
File mavenLib = new File( mavenHome, "lib" );
125128
File[] jarFiles = mavenLib.listFiles( ( dir, name ) -> name.endsWith( ".jar" ) );
126129

130+
if ( jarFiles == null )
131+
{
132+
throw new IllegalArgumentException( "Invalid Maven home installation directory: " + mavenHome );
133+
}
134+
127135
for ( File file : jarFiles )
128136
{
129137
try
130138
{
131-
@SuppressWarnings( "deprecation" )
132-
URL url =
133-
new URL( "jar:" + file.toURL().toExternalForm()
134-
+ "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );
135-
136-
Properties properties = new Properties();
137-
properties.load( url.openStream() );
138-
String version = StringUtils.trim( properties.getProperty( "version" ) );
139-
if ( version != null )
139+
URL url = new URL( "jar:" + file.toURI().toURL().toExternalForm()
140+
+ "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );
141+
142+
try ( InputStream in = url.openStream() )
140143
{
141-
return version;
144+
Properties properties = new Properties();
145+
properties.load( in );
146+
String version = StringUtils.trim( properties.getProperty( "version" ) );
147+
if ( version != null )
148+
{
149+
return version;
150+
}
142151
}
143152
}
144-
catch ( MalformedURLException e )
145-
{
146-
// ignore
147-
}
148-
catch ( IOException e )
153+
catch ( FileNotFoundException | MalformedURLException e )
149154
{
150155
// ignore
151156
}
@@ -251,7 +256,7 @@ static List<Integer> parseVersion( String version )
251256

252257
static int compareVersions( List<Integer> version1, List<Integer> version2 )
253258
{
254-
for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator();; )
259+
for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
255260
{
256261
if ( !it1.hasNext() )
257262
{

src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@
1919
* under the License.
2020
*/
2121

22-
import static org.junit.Assert.assertEquals;
23-
import static org.junit.Assert.assertFalse;
24-
import static org.junit.Assert.assertTrue;
25-
import static org.mockito.Mockito.mock;
26-
import static org.mockito.Mockito.when;
27-
import static org.mockito.Mockito.isA;
28-
22+
import java.io.File;
23+
import java.io.IOException;
2924
import java.util.ArrayList;
3025
import java.util.Arrays;
3126
import java.util.Collections;
@@ -36,6 +31,15 @@
3631
import org.apache.maven.toolchain.ToolchainPrivate;
3732
import org.junit.Test;
3833

34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatCode;
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertFalse;
38+
import static org.junit.Assert.assertTrue;
39+
import static org.mockito.Mockito.isA;
40+
import static org.mockito.Mockito.mock;
41+
import static org.mockito.Mockito.when;
42+
3943
/**
4044
* Tests {@link SelectorUtils}.
4145
*
@@ -120,26 +124,45 @@ public void testIsMatchingToolchain() throws Exception
120124
when( jdkMismatch.getType() ).thenReturn( "jdk" );
121125

122126
when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
123-
.thenReturn( new ToolchainPrivate[] { jdkMatching } );
127+
.thenReturn( new ToolchainPrivate[] {jdkMatching} );
124128
assertTrue( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );
125129

126130
when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
127-
.thenReturn( new ToolchainPrivate[] { jdkMismatch } );
131+
.thenReturn( new ToolchainPrivate[] {jdkMismatch} );
128132
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );
129133

130134
when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
131-
.thenReturn( new ToolchainPrivate[] { jdkMatching, jdkMismatch, jdkMatching } );
135+
.thenReturn( new ToolchainPrivate[] {jdkMatching, jdkMismatch, jdkMatching} );
132136
assertTrue( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );
133137

134138
when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
135-
.thenReturn( new ToolchainPrivate[0] );
139+
.thenReturn( new ToolchainPrivate[0] );
136140
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );
137141

138142
when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
139-
.thenReturn( new ToolchainPrivate[] { jdkMatching } );
143+
.thenReturn( new ToolchainPrivate[] {jdkMatching} );
140144
when( toolchainPrivateManager.getToolchainPrivates( "maven" ) )
141-
.thenReturn( new ToolchainPrivate[0] );
145+
.thenReturn( new ToolchainPrivate[0] );
142146
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Arrays.asList( openJdk9, maven360 ) ) );
143147
}
144148

149+
@Test
150+
public void mavenVersionForNotExistingMavenHomeThrowException()
151+
{
152+
File mavenHome = new File( "not-existing-path" );
153+
154+
assertThatCode( () -> SelectorUtils.getMavenVersion( mavenHome ) )
155+
.isExactlyInstanceOf( IllegalArgumentException.class )
156+
.hasMessage( "Invalid Maven home installation directory: not-existing-path" );
157+
}
158+
159+
@Test
160+
public void mavenVersionFromMavenHome() throws IOException
161+
{
162+
File mavenHome = new File( System.getProperty( "maven.home" ) );
163+
164+
String mavenVersion = SelectorUtils.getMavenVersion( mavenHome );
165+
166+
assertThat( mavenVersion ).isNotBlank();
167+
}
145168
}

0 commit comments

Comments
 (0)