Skip to content

Commit

Permalink
[MINVOKER-297] NPE when non-existing Maven Home
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Feb 28, 2022
1 parent 4d0d70c commit 41af418
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 41 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ under the License.
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ public void execute()
return;
}

setupActualMavenVersion();

handleScriptRunnerWithScriptClassPath();

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

}

private void setupActualMavenVersion() throws MojoExecutionException
{
if ( mavenHome != null )
{
try
{
actualMavenVersion = SelectorUtils.getMavenVersion( mavenHome );
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
else
{
actualMavenVersion = SelectorUtils.getMavenVersion();
}
}

/**
* Find the latest lastModified recursively within a directory structure.
*
Expand Down Expand Up @@ -912,6 +933,7 @@ private void handleScriptRunnerWithScriptClassPath()
scriptRunner = new ScriptRunner( );
scriptRunner.setScriptEncoding( encoding );
scriptRunner.setGlobalVariable( "localRepositoryPath", localRepositoryPath );
scriptRunner.setGlobalVariable( "mavenVersion", actualMavenVersion );
if ( scriptVariables != null )
{
scriptVariables.forEach( ( key, value ) -> scriptRunner.setGlobalVariable( key, value ) );
Expand Down Expand Up @@ -1267,16 +1289,6 @@ private void runBuilds( final File projectsDir, List<BuildJob> buildJobs, int ru

final File mergedSettingsFile = mergeSettings( interpolatedSettingsFile );

if ( mavenHome != null )
{
actualMavenVersion = SelectorUtils.getMavenVersion( mavenHome );
}
else
{
actualMavenVersion = SelectorUtils.getMavenVersion();
}
scriptRunner.setGlobalVariable( "mavenVersion", actualMavenVersion );

final CharSequence actualJreVersion;
// @todo if ( javaVersions ) ... to be picked up from toolchains
if ( javaHome != null )
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -109,7 +111,8 @@ static String getMavenVersion()
// if this ever changes, we will have to revisit this code.
Properties properties = new Properties();
// CHECKSTYLE_OFF: LineLength
properties.load( MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
properties.load( MavenProject.class.getClassLoader()
.getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) );
// CHECKSTYLE_ON: LineLength
return StringUtils.trim( properties.getProperty( "version" ) );
}
Expand All @@ -119,33 +122,35 @@ static String getMavenVersion()
}
}

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

if ( jarFiles == null )
{
throw new IOException( "Invalid Maven home installation directory: " + mavenHome );
}

for ( File file : jarFiles )
{
try
{
@SuppressWarnings( "deprecation" )
URL url =
new URL( "jar:" + file.toURL().toExternalForm()
+ "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );

Properties properties = new Properties();
properties.load( url.openStream() );
String version = StringUtils.trim( properties.getProperty( "version" ) );
if ( version != null )
URL url = new URL( "jar:" + file.toURI().toURL().toExternalForm()
+ "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" );

try ( InputStream in = url.openStream() )
{
return version;
Properties properties = new Properties();
properties.load( in );
String version = StringUtils.trim( properties.getProperty( "version" ) );
if ( version != null )
{
return version;
}
}
}
catch ( MalformedURLException e )
{
// ignore
}
catch ( IOException e )
catch ( FileNotFoundException | MalformedURLException e )
{
// ignore
}
Expand Down Expand Up @@ -251,7 +256,7 @@ static List<Integer> parseVersion( String version )

static int compareVersions( List<Integer> version1, List<Integer> version2 )
{
for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator();; )
for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator(); ; )
{
if ( !it1.hasNext() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@
* under the License.
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.isA;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -36,6 +31,15 @@
import org.apache.maven.toolchain.ToolchainPrivate;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Tests {@link SelectorUtils}.
*
Expand Down Expand Up @@ -120,26 +124,45 @@ public void testIsMatchingToolchain() throws Exception
when( jdkMismatch.getType() ).thenReturn( "jdk" );

when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
.thenReturn( new ToolchainPrivate[] { jdkMatching } );
.thenReturn( new ToolchainPrivate[] {jdkMatching} );
assertTrue( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );

when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
.thenReturn( new ToolchainPrivate[] { jdkMismatch } );
.thenReturn( new ToolchainPrivate[] {jdkMismatch} );
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );

when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
.thenReturn( new ToolchainPrivate[] { jdkMatching, jdkMismatch, jdkMatching } );
.thenReturn( new ToolchainPrivate[] {jdkMatching, jdkMismatch, jdkMatching} );
assertTrue( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );

when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
.thenReturn( new ToolchainPrivate[0] );
.thenReturn( new ToolchainPrivate[0] );
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Collections.singleton( openJdk9 ) ) );

when( toolchainPrivateManager.getToolchainPrivates( "jdk" ) )
.thenReturn( new ToolchainPrivate[] { jdkMatching } );
.thenReturn( new ToolchainPrivate[] {jdkMatching} );
when( toolchainPrivateManager.getToolchainPrivates( "maven" ) )
.thenReturn( new ToolchainPrivate[0] );
.thenReturn( new ToolchainPrivate[0] );
assertFalse( SelectorUtils.isToolchain( toolchainPrivateManager, Arrays.asList( openJdk9, maven360 ) ) );
}

@Test
public void mavenVersionForNotExistingMavenHomeThrowException()
{
File mavenHome = new File( "not-existing-path" );

assertThatCode( () -> SelectorUtils.getMavenVersion( mavenHome ) )
.isExactlyInstanceOf( IOException.class )
.hasMessage( "Invalid Maven home installation directory: not-existing-path" );
}

@Test
public void mavenVersionFromMavenHome() throws IOException
{
File mavenHome = new File( System.getProperty( "maven.home" ) );

String mavenVersion = SelectorUtils.getMavenVersion( mavenHome );

assertThat( mavenVersion ).isNotBlank();
}
}

0 comments on commit 41af418

Please sign in to comment.