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

commit fc473298ed3b8c9e98a639ca50bc16016b00cc02
Author: Arturo Bernal <[email protected]>
AuthorDate: Sun Nov 15 11:36:41 2026 +0100

    Issue #282 Skip connecting to a daemon for --version
    
    (cherry picked from commits 2962aa80fa637e277fb552544d70c6d760b025ff,
    ad4905619ce34a6dbaf192a507aac9fa30942798,
    9219c77d854804532816efef79e50806c84de79a and
    eaaa2e5a)
    Adapted: 1.x DefaultClient lives in java-mvnd, so the output is built by a 
new
    VersionInfo class in client/src/main/java with a unit test. It also keeps 
the
    Java version, locale and OS lines of mvn -v and reads the Maven build number
    from maven-core in mvn/lib, which master's output drops.
---
 .../org/mvndaemon/mvnd/client/DefaultClient.java   |  28 +++-
 .../org/mvndaemon/mvnd/client/BuildProperties.java |  15 +-
 .../org/mvndaemon/mvnd/client/VersionInfo.java     | 175 +++++++++++++++++++++
 .../org/mvndaemon/mvnd/client/build.properties     |   1 +
 .../org/mvndaemon/mvnd/client/VersionInfoTest.java | 141 +++++++++++++++++
 .../org/mvndaemon/mvnd/it/ExtensionsNativeIT.java  |  16 +-
 .../org/mvndaemon/mvnd/it/VersionNativeIT.java     |   7 +-
 7 files changed, 377 insertions(+), 6 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 fb42d851..a15f78b8 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
@@ -287,9 +287,33 @@ public class DefaultClient implements Client {
             output.accept(Message.log(v));
             // Print terminal information
             output.describeTerminal();
+            if (VersionInfo.isPlainVersionRequest(args)) {
+                /*
+                 * A plain -v/--version makes Maven print its own version and 
return without running a build, so
+                 * a daemon round trip cannot change the answer. Print the 
same lines client-side instead of
+                 * connecting to (and possibly spawning) a daemon, still 
without the client module depending on
+                 * any Maven artifact. With -q, --help or an mvnd command such 
as --status the request continues
+                 * to the daemon or to the handling below.
+                 */
+                Path javaHome;
+                try {
+                    javaHome = parameters.javaHome();
+                } catch (RuntimeException e) {
+                    javaHome = null; // no JDK found: report the client JVM 
instead
+                }
+                for (String line : VersionInfo.lines(
+                        parameters.mvndHome().resolve("mvn").resolve("lib"),
+                        buildProperties.getMavenVersion(),
+                        parameters.mvndHome().resolve("mvn"),
+                        javaHome,
+                        System.getProperties())) {
+                    output.accept(Message.log(line));
+                }
+                return DefaultResult.success(argv);
+            }
             /*
-             * Do not return, rather pass -v to the server so that the client 
module does not need to depend on any
-             * Maven artifacts
+             * Do not return, rather pass -v/-V/-X to the server so that the 
client module does not need to depend
+             * on any Maven artifacts
              */
         }
 
diff --git 
a/client/src/main/java/org/mvndaemon/mvnd/client/BuildProperties.java 
b/client/src/main/java/org/mvndaemon/mvnd/client/BuildProperties.java
index 5c3aaa47..893f6938 100644
--- a/client/src/main/java/org/mvndaemon/mvnd/client/BuildProperties.java
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/BuildProperties.java
@@ -41,25 +41,36 @@ public class BuildProperties {
                 buildProperties.getProperty("version"),
                 buildProperties.getProperty("revision"),
                 buildProperties.getProperty("os.detected.name"),
-                buildProperties.getProperty("os.detected.arch"));
+                buildProperties.getProperty("os.detected.arch"),
+                buildProperties.getProperty("maven.version"));
     }
 
     private final String version;
     private final String osName;
     private final String osArch;
     private final String revision;
+    private final String mavenVersion;
 
-    public BuildProperties(String version, String revision, String os, String 
arch) {
+    public BuildProperties(String version, String revision, String os, String 
arch, String mavenVersion) {
         this.version = version;
         this.revision = revision;
         this.osName = os;
         this.osArch = arch;
+        this.mavenVersion = mavenVersion;
     }
 
     public String getVersion() {
         return version;
     }
 
+    /**
+     * The version of Apache Maven embedded in this mvnd distribution, known 
at mvnd's own build time rather
+     * than reported by a running daemon.
+     */
+    public String getMavenVersion() {
+        return mavenVersion;
+    }
+
     public String getOsName() {
         return osName;
     }
diff --git a/client/src/main/java/org/mvndaemon/mvnd/client/VersionInfo.java 
b/client/src/main/java/org/mvndaemon/mvnd/client/VersionInfo.java
new file mode 100644
index 00000000..8b5bbfb9
--- /dev/null
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/VersionInfo.java
@@ -0,0 +1,175 @@
+/*
+ * 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.io.IOException;
+import java.io.InputStream;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.mvndaemon.mvnd.common.Environment;
+
+/**
+ * Builds the lines that {@code mvnd --version} prints in place of Maven, in 
the same layout as {@code mvn --version},
+ * without needing a daemon or any Maven class on the client classpath.
+ */
+final class VersionInfo {
+
+    private VersionInfo() {}
+
+    /**
+     * Whether the arguments ask for nothing but the version, so that it can 
be answered without a daemon. A
+     * quiet or help request, or an mvnd command such as {@code --status}, 
still takes the regular path.
+     */
+    static boolean isPlainVersionRequest(List<String> args) {
+        return Environment.MAVEN_VERSION.hasCommandLineOption(args)
+                && !Environment.STATUS.hasCommandLineOption(args)
+                && !Environment.STOP.hasCommandLineOption(args)
+                && !Environment.PURGE.hasCommandLineOption(args)
+                && !Environment.COMPLETION.hasCommandLineOption(args)
+                && !args.contains("-q")
+                && !args.contains("--quiet")
+                && !args.contains("-h")
+                && !args.contains("--help");
+    }
+
+    /**
+     * Mirrors {@code CLIReportingUtils.showVersion()} of Maven 3.9.
+     *
+     * @param mavenLib the {@code mvn/lib} directory of the distribution, may 
be {@code null}
+     * @param embeddedMavenVersion the Maven version recorded when mvnd was 
built, used if the Maven jar cannot be read
+     * @param mavenHome the value of {@code maven.home} the daemon runs with, 
i.e. the {@code mvn} directory of the
+     *     distribution
+     * @param javaHome the Java home of the JVM the daemon runs in; if {@code 
null}, the client JVM is described
+     * @param sys the client JVM system properties
+     */
+    static List<String> lines(
+            Path mavenLib, String embeddedMavenVersion, Path mavenHome, Path 
javaHome, Properties sys) {
+        List<String> lines = new ArrayList<>();
+        Properties maven = readMavenBuildProperties(mavenLib);
+        lines.add(mavenLine(maven, embeddedMavenVersion));
+        lines.add(maven.getProperty("distributionShortName", "Maven") + " 
home: " + mavenHome);
+
+        String javaVersion;
+        String javaVendor;
+        String runtime;
+        if (javaHome != null) {
+            Properties release = readProperties(javaHome.resolve("release"));
+            javaVersion = unquote(release.getProperty("JAVA_VERSION"), 
"<unknown Java version>");
+            javaVendor = unquote(release.getProperty("IMPLEMENTOR"), "<unknown 
vendor>");
+            runtime = javaHome.toString();
+        } else {
+            javaVersion = sys.getProperty("java.version", "<unknown Java 
version>");
+            javaVendor = sys.getProperty("java.vendor", "<unknown vendor>");
+            runtime = sys.getProperty("java.home", "<unknown runtime>");
+        }
+        lines.add("Java version: " + javaVersion + ", vendor: " + javaVendor + 
", runtime: " + runtime);
+
+        lines.add("Default locale: " + Locale.getDefault() + ", platform 
encoding: "
+                + sys.getProperty("file.encoding", "<unknown encoding>"));
+
+        String osName = sys.getProperty("os.name", "");
+        lines.add("OS name: \"" + osName.toLowerCase(Locale.ENGLISH) + "\", 
version: \""
+                + sys.getProperty("os.version", 
"").toLowerCase(Locale.ENGLISH) + "\", arch: \""
+                + sys.getProperty("os.arch", "").toLowerCase(Locale.ENGLISH) + 
"\", family: \"" + osFamily(osName)
+                + "\"");
+        return lines;
+    }
+
+    private static Properties readMavenBuildProperties(Path mavenLib) {
+        if (mavenLib != null && Files.isDirectory(mavenLib)) {
+            try (DirectoryStream<Path> jars = 
Files.newDirectoryStream(mavenLib, "maven-core-*.jar")) {
+                for (Path jar : jars) {
+                    Properties p = readFromJar(jar, 
"org/apache/maven/messages/build.properties");
+                    if (p.getProperty("version") != null) {
+                        return p;
+                    }
+                }
+            } catch (IOException e) {
+                // fall through to the recorded version
+            }
+        }
+        return new Properties();
+    }
+
+    private static String mavenLine(Properties maven, String 
embeddedMavenVersion) {
+        String name = maven.getProperty("distributionName", "Apache Maven");
+        String version = maven.getProperty("version");
+        String build = maven.getProperty("buildNumber");
+        if (version == null) {
+            return name + " " + embeddedMavenVersion + " (embedded)";
+        }
+        return name + " " + version + (build != null && 
!build.trim().isEmpty() ? " (" + build + ")" : "");
+    }
+
+    private static Properties readFromJar(Path jar, String entryName) {
+        Properties p = new Properties();
+        try (ZipFile zip = new ZipFile(jar.toFile())) {
+            ZipEntry entry = zip.getEntry(entryName);
+            if (entry != null) {
+                try (InputStream is = zip.getInputStream(entry)) {
+                    p.load(is);
+                }
+            }
+        } catch (IOException e) {
+            // empty properties
+        }
+        return p;
+    }
+
+    private static Properties readProperties(Path file) {
+        Properties p = new Properties();
+        if (Files.isRegularFile(file)) {
+            try (InputStream is = Files.newInputStream(file)) {
+                p.load(is);
+            } catch (IOException e) {
+                // empty properties
+            }
+        }
+        return p;
+    }
+
+    private static String unquote(String value, String fallback) {
+        if (value == null) {
+            return fallback;
+        }
+        String v = value.trim();
+        if (v.length() >= 2 && v.startsWith("\"") && v.endsWith("\"")) {
+            v = v.substring(1, v.length() - 1);
+        }
+        return v.isEmpty() ? fallback : v;
+    }
+
+    static String osFamily(String osName) {
+        String n = osName.toLowerCase(Locale.ENGLISH);
+        if (n.contains("windows")) {
+            return "windows";
+        } else if (n.contains("mac")) {
+            return "mac";
+        }
+        return "unix";
+    }
+}
diff --git 
a/client/src/main/resources/org/mvndaemon/mvnd/client/build.properties 
b/client/src/main/resources/org/mvndaemon/mvnd/client/build.properties
index 3f441a55..3c14e572 100644
--- a/client/src/main/resources/org/mvndaemon/mvnd/client/build.properties
+++ b/client/src/main/resources/org/mvndaemon/mvnd/client/build.properties
@@ -17,5 +17,6 @@
 
 revision=${buildNumber}
 version=${project.version}
+maven.version=${maven.version}
 os.detected.name=${os.detected.name}
 os.detected.arch=${os.detected.arch}
\ No newline at end of file
diff --git 
a/client/src/test/java/org/mvndaemon/mvnd/client/VersionInfoTest.java 
b/client/src/test/java/org/mvndaemon/mvnd/client/VersionInfoTest.java
new file mode 100644
index 00000000..8faf085d
--- /dev/null
+++ b/client/src/test/java/org/mvndaemon/mvnd/client/VersionInfoTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class VersionInfoTest {
+
+    private static Properties sys() {
+        Properties p = new Properties();
+        p.setProperty("java.version", "11.0.1");
+        p.setProperty("java.vendor", "ClientVendor");
+        p.setProperty("java.home", "/client/jdk");
+        p.setProperty("file.encoding", "UTF-8");
+        p.setProperty("os.name", "Mac OS X");
+        p.setProperty("os.version", "14.1");
+        p.setProperty("os.arch", "aarch64");
+        return p;
+    }
+
+    @Test
+    void readsMavenAndJavaDetailsFromTheDistribution(@TempDir Path dir) throws 
Exception {
+        Path lib = Files.createDirectories(dir.resolve("mvn/lib"));
+        try (OutputStream os = 
Files.newOutputStream(lib.resolve("maven-core-3.9.9.jar"));
+                ZipOutputStream zip = new ZipOutputStream(os)) {
+            zip.putNextEntry(new 
ZipEntry("org/apache/maven/messages/build.properties"));
+            
zip.write("version=3.9.9\nbuildNumber=abc123\ndistributionName=Apache Maven\n"
+                    .getBytes(StandardCharsets.UTF_8));
+            zip.closeEntry();
+        }
+        Path jdk = Files.createDirectories(dir.resolve("jdk"));
+        Files.write(
+                jdk.resolve("release"),
+                "JAVA_VERSION=\"17.0.20\"\nIMPLEMENTOR=\"Amazon.com 
Inc.\"\n".getBytes(StandardCharsets.UTF_8));
+
+        Path mavenHome = dir.resolve("mvn");
+        List<String> lines = VersionInfo.lines(lib, "0.0", mavenHome, jdk, 
sys());
+
+        assertEquals("Apache Maven 3.9.9 (abc123)", lines.get(0));
+        assertEquals("Maven home: " + mavenHome, lines.get(1));
+        assertEquals("Java version: 17.0.20, vendor: Amazon.com Inc., runtime: 
" + jdk, lines.get(2));
+        assertTrue(lines.get(3).startsWith("Default locale: "));
+        assertTrue(lines.get(3).endsWith(", platform encoding: UTF-8"));
+        assertEquals("OS name: \"mac os x\", version: \"14.1\", arch: 
\"aarch64\", family: \"mac\"", lines.get(4));
+        assertEquals(5, lines.size());
+    }
+
+    @Test
+    void fallsBackToTheEmbeddedVersionAndClientJvm(@TempDir Path dir) {
+        List<String> lines = VersionInfo.lines(dir.resolve("missing"), 
"3.9.16", dir, null, sys());
+
+        assertEquals("Apache Maven 3.9.16 (embedded)", lines.get(0));
+        assertEquals("Java version: 11.0.1, vendor: ClientVendor, runtime: 
/client/jdk", lines.get(2));
+    }
+
+    @Test
+    void partialReleaseFileNeverFallsBackToTheClientJvm(@TempDir Path dir) 
throws Exception {
+        Path jdk = Files.createDirectories(dir.resolve("jdk"));
+        Files.write(jdk.resolve("release"), 
"JAVA_VERSION=\"17.0.20\"\n".getBytes(StandardCharsets.UTF_8));
+
+        List<String> lines = VersionInfo.lines(null, "3.9.16", dir, jdk, 
sys());
+        assertEquals("Java version: 17.0.20, vendor: <unknown vendor>, 
runtime: " + jdk, lines.get(2));
+
+        Path noRelease = Files.createDirectories(dir.resolve("jdk2"));
+        lines = VersionInfo.lines(null, "3.9.16", dir, noRelease, sys());
+        assertEquals(
+                "Java version: <unknown Java version>, vendor: <unknown 
vendor>, runtime: " + noRelease, lines.get(2));
+    }
+
+    @Test
+    void platformEncodingIsFileEncodingOnly() {
+        Properties sys = sys();
+        sys.setProperty("native.encoding", "ISO-8859-2");
+        assertTrue(VersionInfo.lines(null, "3.9.16", Paths.get("m"), null, sys)
+                .get(3)
+                .endsWith(", platform encoding: UTF-8"));
+        sys.remove("file.encoding");
+        assertTrue(VersionInfo.lines(null, "3.9.16", Paths.get("m"), null, sys)
+                .get(3)
+                .endsWith(", platform encoding: <unknown encoding>"));
+    }
+
+    @Test
+    void osLineIsLowerCased() {
+        Properties sys = sys();
+        sys.setProperty("os.version", "10.0 SP1");
+        sys.setProperty("os.arch", "AMD64");
+        assertEquals(
+                "OS name: \"mac os x\", version: \"10.0 sp1\", arch: 
\"amd64\", family: \"mac\"",
+                VersionInfo.lines(null, "3.9.16", Paths.get("m"), null, 
sys).get(4));
+    }
+
+    @Test
+    void plainVersionRequest() {
+        assertTrue(VersionInfo.isPlainVersionRequest(Arrays.asList("-v")));
+        
assertTrue(VersionInfo.isPlainVersionRequest(Arrays.asList("--version", "-V")));
+        for (String other : Arrays.asList("-q", "--quiet", "-h", "--help", 
"--status", "--stop", "--purge")) {
+            assertFalse(VersionInfo.isPlainVersionRequest(Arrays.asList("-v", 
other)), other);
+        }
+        assertFalse(VersionInfo.isPlainVersionRequest(Arrays.asList("clean")));
+    }
+
+    @Test
+    void osFamily() {
+        assertEquals("windows", VersionInfo.osFamily("Windows 11"));
+        assertEquals("mac", VersionInfo.osFamily("Mac OS X"));
+        assertEquals("unix", VersionInfo.osFamily("Linux"));
+    }
+}
diff --git 
a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionsNativeIT.java 
b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionsNativeIT.java
index 66ffee87..aae842b4 100644
--- 
a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionsNativeIT.java
+++ 
b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/ExtensionsNativeIT.java
@@ -45,6 +45,7 @@ class ExtensionsNativeIT {
     @Inject
     TestRegistry registry;
 
+    /** {@code -v}/{@code --version} completes without spawning (or otherwise 
using) a daemon at all. */
     @Test
     void version() throws IOException, InterruptedException {
         registry.killAll();
@@ -52,13 +53,26 @@ class ExtensionsNativeIT {
 
         final TestClientOutput o = new TestClientOutput();
         client.execute(o, "-v").assertSuccess();
+        assertDaemonRegistrySize(0);
+
+        client.execute(o, "-v").assertSuccess();
+        assertDaemonRegistrySize(0);
+    }
+
+    @Test
+    void extensionsReachTheDaemon() throws IOException, InterruptedException {
+        registry.killAll();
+        assertDaemonRegistrySize(0);
+
+        final TestClientOutput o = new TestClientOutput();
+        client.execute(o, "validate").assertSuccess();
         assertDaemonRegistrySize(1);
         DaemonInfo daemon = registry.getAll().iterator().next();
         
assertTrue(daemon.getOptions().contains("mvnd.coreExtensions=fr.jcgay.maven:maven-profiler:3.0"));
 
         registry.awaitIdle(daemon.getId());
 
-        client.execute(o, "-v").assertSuccess();
+        client.execute(o, "validate").assertSuccess();
         assertDaemonRegistrySize(1);
     }
 
diff --git 
a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/VersionNativeIT.java 
b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/VersionNativeIT.java
index 6f787616..9a64dc22 100644
--- a/integration-tests/src/test/java/org/mvndaemon/mvnd/it/VersionNativeIT.java
+++ b/integration-tests/src/test/java/org/mvndaemon/mvnd/it/VersionNativeIT.java
@@ -53,7 +53,12 @@ class VersionNativeIT {
                                         + " native client"
                                 : "JVM client")
                         + "\\E",
-                "\\QMaven home: " + parameters.mvndHome() + "\\E");
+                "Apache Maven [0-9].*",
+                "\\QMaven home: " + parameters.mvndHome().resolve("mvn")
+                        + "\\E'", // the closing quote of the message payload: 
not the mvnd home itself
+                "Java version: .*, vendor: .*, runtime: .*",
+                "Default locale: .*, platform encoding: .*",
+                "OS name: \".*\", version: \".*\", arch: \".*\", family: 
\".*\"");
     }
 
     protected boolean isNative() {

Reply via email to