You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2022/12/26 17:01:04 UTC

[maven] 01/01: [MNG-5857] Arguments from command line should override those in .mvn/maven.config

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MNG-5857_alt-1
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 46dbf899d4ade5a93d374c3463f53a150b0946db
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Oct 24 19:46:49 2021 +0200

    [MNG-5857] Arguments from command line should override those in .mvn/maven.config
---
 apache-maven/src/assembly/shared/init              |   1 +
 apache-maven/src/assembly/shared/init.cmd          |   7 +
 .../main/java/org/apache/maven/cli/MavenCli.java   |  65 +--------
 .../java/org/apache/maven/cli/MavenCliTest.java    | 155 ---------------------
 .../test/projects/config-illegal/.mvn/maven.config |   1 -
 .../src/test/projects/config/.mvn/maven.config     |   3 -
 .../mavenConfigProperties/.mvn/maven.config        |   5 -
 7 files changed, 9 insertions(+), 228 deletions(-)

diff --git a/apache-maven/src/assembly/shared/init b/apache-maven/src/assembly/shared/init
index d5bd529b6..934e039e8 100755
--- a/apache-maven/src/assembly/shared/init
+++ b/apache-maven/src/assembly/shared/init
@@ -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"`"
diff --git a/apache-maven/src/assembly/shared/init.cmd b/apache-maven/src/assembly/shared/init.cmd
index e41e093b2..7742d6921 100755
--- a/apache-maven/src/assembly/shared/init.cmd
+++ b/apache-maven/src/assembly/shared/init.cmd
@@ -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
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 509b02ede..c27e1fe77 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
@@ -27,8 +27,6 @@ import java.io.FileOutputStream;
 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;
@@ -44,7 +42,6 @@ import java.util.regex.Pattern;
 
 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;
@@ -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;
@@ -339,36 +334,8 @@ public class MavenCli {
 
         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);
@@ -392,36 +359,6 @@ public class MavenCli {
         }
     }
 
-    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
      */
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 1f1da2e7c..e4f320ce0 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
@@ -163,161 +163,6 @@ public class MavenCliTest {
         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");
diff --git a/maven-embedder/src/test/projects/config-illegal/.mvn/maven.config b/maven-embedder/src/test/projects/config-illegal/.mvn/maven.config
deleted file mode 100644
index 8541464a7..000000000
--- a/maven-embedder/src/test/projects/config-illegal/.mvn/maven.config
+++ /dev/null
@@ -1 +0,0 @@
-deploy
diff --git a/maven-embedder/src/test/projects/config/.mvn/maven.config b/maven-embedder/src/test/projects/config/.mvn/maven.config
deleted file mode 100644
index 280d57794..000000000
--- a/maven-embedder/src/test/projects/config/.mvn/maven.config
+++ /dev/null
@@ -1,3 +0,0 @@
--T8
---builder
-multithreaded
diff --git a/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config b/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config
deleted file mode 100644
index 82570230f..000000000
--- a/maven-embedder/src/test/projects/mavenConfigProperties/.mvn/maven.config
+++ /dev/null
@@ -1,5 +0,0 @@
--T
-3
--Drevision=1.3.0
-"-Dlabel=Apache Maven"
-