Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-5857] Arguments from command line should override those in .mvn/maven.config (alternative 1) #936

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apache-maven/src/assembly/shared/init
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ concat_lines() {

MAVEN_PROJECTBASEDIR="`find_maven_basedir "$@"`"
MAVEN_OPTS="$MAVEN_OPTS `concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config"`"
MAVEN_ARGS="$MAVEN_ARGS `concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/maven.config"`"
7 changes: 7 additions & 0 deletions apache-maven/src/assembly/shared/init.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do s

:endReadJvmConfig

if not exist "%MAVEN_PROJECTBASEDIR%\.mvn\maven.config" goto endReadMavenConfig

@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\maven.config") do set MAVEN_CONFIG_MAVEN_PROPS=!MAVEN_CONFIG_MAVEN_PROPS! %%a
@endlocal & set MAVEN_ARGS=%MAVEN_ARGS% %MAVEN_CONFIG_MAVEN_PROPS%

:endReadMavenConfig
65 changes: 1 addition & 64 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -44,7 +42,6 @@

import com.google.inject.AbstractModule;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.commons.lang3.math.NumberUtils;
Expand Down Expand Up @@ -147,8 +144,6 @@ public class MavenCli {

private static final String EXTENSIONS_FILENAME = ".mvn/extensions.xml";

private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config";

public static final String STYLE_COLOR_PROPERTY = "style.color";

private ClassWorld classWorld;
Expand Down Expand Up @@ -339,36 +334,8 @@ void cli(CliRequest cliRequest) throws Exception {

cliManager = new CLIManager();

List<String> args = new ArrayList<>();
CommandLine mavenConfig = null;
try {
File configFile = new File(cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG);

if (configFile.isFile()) {
for (String arg : Files.readAllLines(configFile.toPath(), Charset.defaultCharset())) {
if (!arg.isEmpty()) {
args.add(arg);
}
}

mavenConfig = cliManager.parse(args.toArray(new String[0]));
List<?> unrecognized = mavenConfig.getArgList();
if (!unrecognized.isEmpty()) {
throw new ParseException("Unrecognized maven.config entries: " + unrecognized);
}
}
} catch (ParseException e) {
System.err.println("Unable to parse maven.config: " + e.getMessage());
cliManager.displayHelp(System.out);
throw e;
}

try {
if (mavenConfig == null) {
cliRequest.commandLine = cliManager.parse(cliRequest.args);
} else {
cliRequest.commandLine = cliMerge(cliManager.parse(cliRequest.args), mavenConfig);
}
cliRequest.commandLine = cliManager.parse(cliRequest.args);
} catch (ParseException e) {
System.err.println("Unable to parse command line options: " + e.getMessage());
cliManager.displayHelp(System.out);
Expand All @@ -392,36 +359,6 @@ private void informativeCommands(CliRequest cliRequest) throws ExitException {
}
}

private CommandLine cliMerge(CommandLine mavenArgs, CommandLine mavenConfig) {
CommandLine.Builder commandLineBuilder = new CommandLine.Builder();

// the args are easy, cli first then config file
for (String arg : mavenArgs.getArgs()) {
commandLineBuilder.addArg(arg);
}
for (String arg : mavenConfig.getArgs()) {
commandLineBuilder.addArg(arg);
}

// now add all options, except for -D with cli first then config file
List<Option> setPropertyOptions = new ArrayList<>();
for (Option opt : mavenArgs.getOptions()) {
if (String.valueOf(CLIManager.SET_USER_PROPERTY).equals(opt.getOpt())) {
setPropertyOptions.add(opt);
} else {
commandLineBuilder.addOption(opt);
}
}
for (Option opt : mavenConfig.getOptions()) {
commandLineBuilder.addOption(opt);
}
// finally add the CLI user properties
for (Option opt : setPropertyOptions) {
commandLineBuilder.addOption(opt);
}
return commandLineBuilder.build();
}

/**
* configure logging
*/
Expand Down
155 changes: 0 additions & 155 deletions maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,161 +163,6 @@ public void testCalculateDegreeOfConcurrency() {
assertThrows(IllegalArgumentException.class, () -> cli.calculateDegreeOfConcurrency("0C"));
}

@Test
public void testMavenConfig() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY, new File("src/test/projects/config").getCanonicalPath());
CliRequest request = new CliRequest(new String[0], null);

// read .mvn/maven.config
cli.initialize(request);
cli.cli(request);
assertEquals("multithreaded", request.commandLine.getOptionValue(CLIManager.BUILDER));
assertEquals("8", request.commandLine.getOptionValue(CLIManager.THREADS));

// override from command line
request = new CliRequest(new String[] {"--builder", "foobar"}, null);
cli.cli(request);
assertEquals("foobar", request.commandLine.getOptionValue("builder"));
}

@Test
public void testMavenConfigInvalid() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
new File("src/test/projects/config-illegal").getCanonicalPath());
CliRequest request = new CliRequest(new String[0], null);

cli.initialize(request);
assertThrows(ParseException.class, () -> cli.cli(request));
}

/**
* Read .mvn/maven.config with the following definitions:
* <pre>
* -T
* 3
* -Drevision=1.3.0
* "-Dlabel=Apache Maven"
* </pre>
* and check if the {@code -T 3} option can be overwritten via command line
* argument.
*
* @throws Exception in case of failure.
*/
@Test
public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
new File("src/test/projects/mavenConfigProperties").getCanonicalPath());
CliRequest request = new CliRequest(new String[] {"-T", "5"}, null);

cli.initialize(request);
// read .mvn/maven.config
cli.cli(request);

assertEquals("5", request.commandLine.getOptionValue(CLIManager.THREADS));
}

/**
* Read .mvn/maven.config with the following definitions:
* <pre>
* -T
* 3
* -Drevision=1.3.0
* "-Dlabel=Apache Maven"
* </pre>
* and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
* argument.
*
* @throws Exception
*/
@Test
public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
new File("src/test/projects/mavenConfigProperties").getCanonicalPath());
CliRequest request = new CliRequest(new String[] {"-Drevision=8.1.0"}, null);

cli.initialize(request);
// read .mvn/maven.config
cli.cli(request);
cli.properties(request);

String revision = System.getProperty("revision");
assertEquals("8.1.0", revision);
}

/**
* Read .mvn/maven.config with the following definitions:
* <pre>
* -T
* 3
* -Drevision=1.3.0
* "-Dlabel=Apache Maven"
* </pre>
* and check if the {@code -Drevision-1.3.0} option can be overwritten via command line
* argument.
*
* @throws Exception
*/
@Test
public void testMVNConfigurationCLIRepeatedPropertiesLastWins() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
new File("src/test/projects/mavenConfigProperties").getCanonicalPath());
CliRequest request = new CliRequest(new String[] {"-Drevision=8.1.0", "-Drevision=8.2.0"}, null);

cli.initialize(request);
// read .mvn/maven.config
cli.cli(request);
cli.properties(request);

String revision = System.getProperty("revision");
assertEquals("8.2.0", revision);
}

/**
* Read .mvn/maven.config with the following definitions:
* <pre>
* -T
* 3
* -Drevision=1.3.0
* "-Dlabel=Apache Maven"
* </pre>
* and check if the {@code -Drevision-1.3.0} option can be overwritten via command line argument when there are
* funky arguments present.
*
* @throws Exception
*/
@Test
public void testMVNConfigurationFunkyArguments() throws Exception {
System.setProperty(
MavenCli.MULTIMODULE_PROJECT_DIRECTORY,
new File("src/test/projects/mavenConfigProperties").getCanonicalPath());
CliRequest request = new CliRequest(
new String[] {
"-Drevision=8.1.0", "--file=-Dpom.xml", "\"-Dfoo=bar ", "\"-Dfoo2=bar two\"", "-Drevision=8.2.0"
},
null);

cli.initialize(request);
// read .mvn/maven.config
cli.cli(request);
cli.properties(request);

assertEquals("3", request.commandLine.getOptionValue(CLIManager.THREADS));

String revision = System.getProperty("revision");
assertEquals("8.2.0", revision);

assertEquals("bar ", request.getUserProperties().getProperty("foo"));
assertEquals("bar two", request.getUserProperties().getProperty("foo2"));
assertEquals("Apache Maven", request.getUserProperties().getProperty("label"));

assertEquals("-Dpom.xml", request.getCommandLine().getOptionValue(CLIManager.ALTERNATE_POM_FILE));
}

@Test
public void testStyleColors() throws Exception {
assumeTrue(MessageUtils.isColorEnabled(), "ANSI not supported");
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions maven-embedder/src/test/projects/config/.mvn/maven.config

This file was deleted.

This file was deleted.