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

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


The following commit(s) were added to refs/heads/maven-3.10.x by this push:
     new db57c679ef Read jar resources without the JVM jar: cache
db57c679ef is described below

commit db57c679ef663df90b94ac1aef8b08d699ca861d
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Wed Oct 28 21:55:05 2026 +0000

    Read jar resources without the JVM jar: cache
    
    The core extension descriptor and lifecycle.xml were read with
    URL.openStream(), which leaves the plugin or extension jar open in the
    JVM-wide jar: cache for the rest of the build (JDK-8239054): it stays
    locked on Windows and is read stale under mvnd once rebuilt.
---
 .../extension/internal/CoreExtensionEntry.java     |  6 +-
 .../maven/plugin/descriptor/PluginDescriptor.java  |  6 +-
 .../plugin/descriptor/PluginDescriptorTest.java    | 73 ++++++++++++++++++++++
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git 
a/maven-core/src/main/java/org/apache/maven/extension/internal/CoreExtensionEntry.java
 
b/maven-core/src/main/java/org/apache/maven/extension/internal/CoreExtensionEntry.java
index f49b004d36..0103192994 100644
--- 
a/maven-core/src/main/java/org/apache/maven/extension/internal/CoreExtensionEntry.java
+++ 
b/maven-core/src/main/java/org/apache/maven/extension/internal/CoreExtensionEntry.java
@@ -22,6 +22,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -83,7 +84,10 @@ public static CoreExtensionEntry discoverFrom(ClassRealm 
loader) {
             Enumeration<URL> urls = 
loader.getResources(BUILDER.getExtensionDescriptorLocation());
             while (urls.hasMoreElements()) {
 
-                try (InputStream is = urls.nextElement().openStream()) {
+                URLConnection connection = urls.nextElement().openConnection();
+                // Uncached, so that closing the stream closes the jar 
(JDK-8239054)
+                connection.setUseCaches(false);
+                try (InputStream is = connection.getInputStream()) {
                     ExtensionDescriptor descriptor = BUILDER.build(is);
                     artifacts.addAll(descriptor.getExportedArtifacts());
                     packages.addAll(descriptor.getExportedPackages());
diff --git 
a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
 
b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
index 80550ddc11..c5ff0c7361 100644
--- 
a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
+++ 
b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java
@@ -25,6 +25,7 @@
 import java.io.Reader;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -370,7 +371,10 @@ private InputStream getDescriptorStream(String descriptor) 
throws IOException {
 
         if (pluginFile.isFile()) {
             try {
-                return new URL("jar:" + pluginFile.toURI() + "!/" + 
descriptor).openStream();
+                URLConnection connection = new URL("jar:" + pluginFile.toURI() 
+ "!/" + descriptor).openConnection();
+                // Uncached, so that closing the stream closes the jar 
(JDK-8239054)
+                connection.setUseCaches(false);
+                return connection.getInputStream();
             } catch (MalformedURLException e) {
                 throw new IllegalStateException(e);
             }
diff --git 
a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorTest.java
 
b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorTest.java
new file mode 100644
index 0000000000..e55999fd91
--- /dev/null
+++ 
b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.apache.maven.plugin.descriptor;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Proxy;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.jar.JarOutputStream;
+import java.util.zip.ZipEntry;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.lifecycle.Lifecycle;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+class PluginDescriptorTest {
+
+    @Test
+    void lifecycleMappingReadsRewrittenJar(@TempDir Path tempDir) throws 
Exception {
+        Path jar = tempDir.resolve("plugin.jar");
+        writeLifecycle(jar, "first");
+        assertNotNull(lifecycleMapping(jar, "first"));
+
+        // Rewritten in place, as a plugin rebuilt in the reactor is.
+        writeLifecycle(jar, "second-and-longer-than-the-first");
+        assertNotNull(lifecycleMapping(jar, 
"second-and-longer-than-the-first"));
+
+        // Nothing keeps the jar open, so it can be deleted (fails on Windows 
while it is open).
+        Files.delete(jar);
+    }
+
+    private static Lifecycle lifecycleMapping(Path jar, String id) throws 
Exception {
+        // Only the file of the plugin artifact is read
+        Artifact artifact = (Artifact) Proxy.newProxyInstance(
+                Artifact.class.getClassLoader(),
+                new Class<?>[] {Artifact.class},
+                (proxy, method, args) -> "getFile".equals(method.getName()) ? 
jar.toFile() : null);
+        PluginDescriptor descriptor = new PluginDescriptor();
+        descriptor.setPluginArtifact(artifact);
+        return descriptor.getLifecycleMapping(id);
+    }
+
+    private static void writeLifecycle(Path jar, String id) throws IOException 
{
+        try (OutputStream out = Files.newOutputStream(jar);
+                JarOutputStream jarOut = new JarOutputStream(out)) {
+            jarOut.putNextEntry(new ZipEntry("META-INF/maven/lifecycle.xml"));
+            jarOut.write(("<lifecycles><lifecycle><id>" + id + 
"</id></lifecycle></lifecycles>")
+                    .getBytes(StandardCharsets.UTF_8));
+            jarOut.closeEntry();
+        }
+    }
+}

Reply via email to