From df67c00fba25a52e39fa43021b52de8a25b6e999 Mon Sep 17 00:00:00 2001 From: Maarten Mulders Date: Fri, 25 Sep 2020 18:01:49 +0200 Subject: [PATCH] [MNG-6991] Restore how the local repository is determined The refactoring of MavenCli.populateRequest introduced a subtle bug. It would select ~/.m2/repository as the local repository instead of something that is configured in ~/.m2/settings.xml. Closes #378. --- .../java/org/apache/maven/cli/MavenCli.java | 19 +++++++--- .../org/apache/maven/cli/MavenCliTest.java | 37 ++++++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 84a04c53fe..fd650f0d36 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -1338,7 +1338,7 @@ public class MavenCli return defaultLocation; } - private MavenExecutionRequest populateRequest( CliRequest cliRequest ) + protected MavenExecutionRequest populateRequest( CliRequest cliRequest ) { return populateRequest( cliRequest, cliRequest.request ); } @@ -1389,7 +1389,11 @@ public class MavenCli request.addActiveProfiles( profileActivation.activeProfiles ); request.addInactiveProfiles( profileActivation.inactiveProfiles ); - request.setLocalRepositoryPath( determineLocalRepositoryPath( request ) ); + final String localRepositoryPath = determineLocalRepositoryPath( request ); + if ( localRepositoryPath != null ) + { + request.setLocalRepositoryPath( localRepositoryPath ); + } // // Builder, concurrency and parallelism @@ -1428,10 +1432,13 @@ public class MavenCli private String determineLocalRepositoryPath( final MavenExecutionRequest request ) { - return request.getUserProperties().getProperty( - MavenCli.LOCAL_REPO_PROPERTY, - request.getSystemProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY ) // null if not found - ); + String userDefinedLocalRepo = request.getUserProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY ); + if ( userDefinedLocalRepo != null ) + { + return userDefinedLocalRepo; + } + + return request.getSystemProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY ); } private File determinePom( final CommandLine commandLine, final String workingDirectory, final File baseDirectory ) diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java index 8c8752796b..e60ee9d175 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java @@ -22,6 +22,9 @@ package org.apache.maven.cli; import static java.util.Arrays.asList; import static org.apache.maven.cli.MavenCli.determineProfileActivation; import static org.apache.maven.cli.MavenCli.determineProjectActivation; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -29,8 +32,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; import static org.mockito.ArgumentMatchers.any; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -47,6 +48,7 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.maven.Maven; import org.apache.maven.eventspy.internal.EventSpyDispatcher; +import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.utils.logging.MessageUtils; import org.apache.maven.toolchain.building.ToolchainsBuildingRequest; @@ -55,6 +57,7 @@ import org.codehaus.plexus.DefaultPlexusContainer; import org.codehaus.plexus.PlexusContainer; import org.eclipse.sisu.plexus.PlexusBeanModule; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; @@ -433,6 +436,36 @@ public class MavenCliTest assertThat( selector, is( "group-a:module" ) ); } + @Test + public void verifyLocalRepositoryPath() + { + MavenCli cli = new MavenCli(); + CliRequest request = new CliRequest( new String[] { }, null ); + request.commandLine = new CommandLine.Builder().build(); + MavenExecutionRequest executionRequest; + + // Use default + executionRequest = cli.populateRequest( request ); + assertThat( executionRequest.getLocalRepositoryPath(), + is( nullValue() ) ); + + // System-properties override default + request.getSystemProperties().setProperty( MavenCli.LOCAL_REPO_PROPERTY, "." + File.separatorChar + "custom1" ); + executionRequest = cli.populateRequest( request ); + assertThat( executionRequest.getLocalRepositoryPath(), + is( notNullValue() ) ); + assertThat( executionRequest.getLocalRepositoryPath().toString(), + is( "." + File.separatorChar + "custom1" ) ); + + // User-properties override system properties + request.getUserProperties().setProperty( MavenCli.LOCAL_REPO_PROPERTY, "." + File.separatorChar + "custom2" ); + executionRequest = cli.populateRequest( request ); + assertThat( executionRequest.getLocalRepositoryPath(), + is( notNullValue() ) ); + assertThat( executionRequest.getLocalRepositoryPath().toString(), + is( "." + File.separatorChar + "custom2" ) ); + } + private MavenProject createMavenProject( String groupId, String artifactId ) { MavenProject project = new MavenProject();