This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch mvnd-1.x
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git
The following commit(s) were added to refs/heads/mvnd-1.x by this push:
new 9dc09210 Provide MAVEN_CMD_LINE_ARGS to the build like the mvn script
does
9dc09210 is described below
commit 9dc092104293a5724686e7201ede4c240a46b715
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 6 22:40:00 2026 +0100
Provide MAVEN_CMD_LINE_ARGS to the build like the mvn script does
Fixes #968
---
.../org/mvndaemon/mvnd/client/DefaultClient.java | 12 +++++-
.../mvndaemon/mvnd/client/MavenCmdLineArgs.java | 48 +++++++++++++++++++++
.../mvnd/client/MavenCmdLineArgsTest.java | 50 ++++++++++++++++++++++
3 files changed, 108 insertions(+), 2 deletions(-)
diff --git
a/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
index 084c5e2c..5d8ecfe5 100644
--- a/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
+++ b/client/src/main/java-mvnd/org/mvndaemon/mvnd/client/DefaultClient.java
@@ -71,9 +71,12 @@ public class DefaultClient implements Client {
private static final Logger LOGGER =
LoggerFactory.getLogger(DefaultClient.class);
private final DaemonParameters parameters;
+ private final List<String> userArgs;
public static void main(String[] argv) throws Exception {
final List<String> args = new ArrayList<>(Arrays.asList(argv));
+ // What the user typed, before mvnd adds or removes options;
MAVEN_CMD_LINE_ARGS is built from it
+ final List<String> userArgs = new ArrayList<>(args);
// Log file
Path logFile = null;
@@ -170,7 +173,7 @@ public class DefaultClient implements Client {
* and --color is not supported there yet. */
args.add("-D" + Environment.MAVEN_COLOR.getProperty() + "=" +
styleColor.name());
- final ExecutionResult result = new
DefaultClient(parameters).execute(output, args);
+ final ExecutionResult result = new DefaultClient(parameters,
userArgs).execute(output, args);
exitCode = result.getExitCode();
} catch (DaemonException.InterruptedException e) {
final AttributedStyle s = new
AttributedStyle().bold().foreground(AttributedStyle.RED);
@@ -227,6 +230,11 @@ public class DefaultClient implements Client {
}
public DefaultClient(DaemonParameters parameters) {
+ this(parameters, null);
+ }
+
+ DefaultClient(DaemonParameters parameters, List<String> userArgs) {
+ this.userArgs = userArgs;
// Those options are needed in order to be able to set the environment
correctly
this.parameters = parameters.withJdkJavaOpts(
"--add-opens java.base/java.io=ALL-UNNAMED "
@@ -370,7 +378,7 @@ public class DefaultClient implements Client {
args,
parameters.userDir().toString(),
parameters.multiModuleProjectDirectory().toString(),
- buildRequestEnvironment()));
+ MavenCmdLineArgs.addTo(buildRequestEnvironment(),
userArgs != null ? userArgs : argv)));
output.accept(Message.buildStatus(
"Connected to daemon " + daemon.getDaemon().getId() +
", scanning for projects..."));
diff --git
a/client/src/main/java/org/mvndaemon/mvnd/client/MavenCmdLineArgs.java
b/client/src/main/java/org/mvndaemon/mvnd/client/MavenCmdLineArgs.java
new file mode 100644
index 00000000..1dd98340
--- /dev/null
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/MavenCmdLineArgs.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.mvndaemon.mvnd.client;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Provides the {@code MAVEN_CMD_LINE_ARGS} environment variable that the
{@code mvn} script exports.
+ */
+final class MavenCmdLineArgs {
+
+ private static final String NAME = "MAVEN_CMD_LINE_ARGS";
+
+ private MavenCmdLineArgs() {}
+
+ /**
+ * Adds {@code MAVEN_CMD_LINE_ARGS} to {@code env}, as the {@code mvn}
script does with
+ * {@code MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"}, so that extensions can
read the command line.
+ * A value already present in {@code env} is kept.
+ */
+ static Map<String, String> addTo(Map<String, String> env, List<String>
args) {
+ if (env.containsKey(NAME)) {
+ return env;
+ }
+ Map<String, String> result = new HashMap<>(env);
+ String mavenConfig = env.get("MAVEN_CONFIG");
+ result.put(NAME, (mavenConfig != null ? mavenConfig : "") + " " +
String.join(" ", args));
+ return result;
+ }
+}
diff --git
a/client/src/test/java/org/mvndaemon/mvnd/client/MavenCmdLineArgsTest.java
b/client/src/test/java/org/mvndaemon/mvnd/client/MavenCmdLineArgsTest.java
new file mode 100644
index 00000000..538ff563
--- /dev/null
+++ b/client/src/test/java/org/mvndaemon/mvnd/client/MavenCmdLineArgsTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.mvndaemon.mvnd.client;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class MavenCmdLineArgsTest {
+
+ @Test
+ void mavenCmdLineArgsFollowsTheMvnScript() {
+ Map<String, String> env =
+ MavenCmdLineArgs.addTo(new HashMap<>(), Arrays.asList("clean",
"install", "-Dversioning.disable=true"));
+ assertEquals(" clean install -Dversioning.disable=true",
env.get("MAVEN_CMD_LINE_ARGS"));
+
+ Map<String, String> base = new HashMap<>();
+ base.put("MAVEN_CONFIG", "-T4");
+ env = MavenCmdLineArgs.addTo(base, Arrays.asList("verify"));
+ assertEquals("-T4 verify", env.get("MAVEN_CMD_LINE_ARGS"));
+ }
+
+ @Test
+ void mavenCmdLineArgsSetByUserIsNotOverridden() {
+ Map<String, String> base = new HashMap<>();
+ base.put("MAVEN_CMD_LINE_ARGS", "custom");
+ Map<String, String> env = MavenCmdLineArgs.addTo(base,
Arrays.asList("clean"));
+ assertEquals("custom", env.get("MAVEN_CMD_LINE_ARGS"));
+ }
+}