[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.
This commit is contained in:
Maarten Mulders 2020-09-25 18:01:49 +02:00
parent 0e3c7a433f
commit df67c00fba
2 changed files with 48 additions and 8 deletions

View File

@ -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 )

View File

@ -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();