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

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f4b1c0  Read the Maven version without the JVM jar: cache
0f4b1c0 is described below

commit 0f4b1c020a1628d5c83666b289416ee3b446a038
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Wed Oct 28 22:04:21 2026 +0000

    Read the Maven version without the JVM jar: cache
    
    getMavenVersion opened every jar under the Maven home's lib directory
    through the JVM-wide jar: cache, which keeps them all open for the rest
    of the build (JDK-8239054): locked on Windows and read stale under mvnd
    once the distribution is unpacked again.
---
 .../maven/plugins/invoker/SelectorUtils.java       |  6 ++++-
 .../maven/plugins/invoker/SelectorUtilsTest.java   | 31 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java 
b/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java
index 1f886d7..3eaef2e 100644
--- a/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java
+++ b/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -114,7 +115,10 @@ class SelectorUtils {
                 URL url = new URL("jar:" + 
file.toURI().toURL().toExternalForm()
                         + 
"!/META-INF/maven/org.apache.maven/maven-core/pom.properties");
 
-                try (InputStream in = url.openStream()) {
+                URLConnection connection = url.openConnection();
+                // Uncached, so that closing the stream closes the jar 
(JDK-8239054)
+                connection.setUseCaches(false);
+                try (InputStream in = connection.getInputStream()) {
                     Properties properties = new Properties();
                     properties.load(in);
                     String str = properties.getProperty("version");
diff --git 
a/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java 
b/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java
index 1c6f569..25bcfde 100644
--- a/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java
+++ b/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java
@@ -19,13 +19,21 @@
 package org.apache.maven.plugins.invoker;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.jar.JarOutputStream;
+import java.util.zip.ZipEntry;
 
 import 
org.apache.maven.plugins.invoker.AbstractInvokerMojo.ToolchainPrivateManager;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
@@ -132,4 +140,27 @@ class SelectorUtilsTest {
 
         assertThat(mavenVersion).isNotBlank();
     }
+
+    @Test
+    void mavenVersionFromRewrittenMavenHome(@TempDir Path mavenHome) throws 
Exception {
+        Path jar = 
Files.createDirectory(mavenHome.resolve("lib")).resolve("maven-core.jar");
+        writeMavenCore(jar, "4.0.0");
+        
assertThat(SelectorUtils.getMavenVersion(mavenHome.toFile())).isEqualTo("4.0.0");
+
+        // Rewritten in place, as a Maven distribution unpacked again by the 
same build is.
+        writeMavenCore(jar, "4.1.0-SNAPSHOT");
+        
assertThat(SelectorUtils.getMavenVersion(mavenHome.toFile())).isEqualTo("4.1.0-SNAPSHOT");
+
+        // Nothing keeps the jar open, so it can be deleted (fails on Windows 
while it is open).
+        Files.delete(jar);
+    }
+
+    private static void writeMavenCore(Path jar, String version) throws 
IOException {
+        try (OutputStream out = Files.newOutputStream(jar);
+                JarOutputStream jarOut = new JarOutputStream(out)) {
+            jarOut.putNextEntry(new 
ZipEntry("META-INF/maven/org.apache.maven/maven-core/pom.properties"));
+            jarOut.write(("version=" + version + 
"\n").getBytes(StandardCharsets.UTF_8));
+            jarOut.closeEntry();
+        }
+    }
 }

Reply via email to