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

sjaranowski pushed a commit to branch fix-toolchains
in repository https://gitbox.apache.org/repos/asf/maven-jar-plugin.git

commit 1fa30e8825ad4ac557967d113f1685a6f9ae8e0e
Author: Slawomir Jaranowski <[email protected]>
AuthorDate: Mon Nov 10 22:01:32 2025 +0100

    Fix detecting java version for toolchains and JDK 1.8
    
    - javac -version in JDK 1.8 output result to standard error
    - we can have multiple line in output
---
 src/it/MJAR-62-toolchain/invoker.properties        |  2 +-
 src/it/MJAR-62-toolchain/pom.xml                   |  2 +-
 .../invoker.properties                             |  2 +-
 src/it/{github-470 => github-470-1.8}/pom.xml      |  2 +-
 .../src/main/java/myproject/HelloWorld.java        | 36 ++++++++++++++++++++++
 src/it/github-470-1.8/verify.groovy                | 26 ++++++++++++++++
 src/it/github-470/invoker.properties               |  2 +-
 src/it/github-470/pom.xml                          |  2 +-
 .../plugins/jar/ToolchainsJdkSpecification.java    | 32 ++++++++++---------
 9 files changed, 86 insertions(+), 20 deletions(-)

diff --git a/src/it/MJAR-62-toolchain/invoker.properties 
b/src/it/MJAR-62-toolchain/invoker.properties
index 9904dcc..75c1c4b 100644
--- a/src/it/MJAR-62-toolchain/invoker.properties
+++ b/src/it/MJAR-62-toolchain/invoker.properties
@@ -15,4 +15,4 @@
 # specific language governing permissions and limitations
 # under the License.
 
-invoker.toolchain.jdk.version = 17
\ No newline at end of file
+invoker.toolchain.jdk.version = [17,18)
\ No newline at end of file
diff --git a/src/it/MJAR-62-toolchain/pom.xml b/src/it/MJAR-62-toolchain/pom.xml
index 6b2648b..7d1787f 100644
--- a/src/it/MJAR-62-toolchain/pom.xml
+++ b/src/it/MJAR-62-toolchain/pom.xml
@@ -44,7 +44,7 @@
         <configuration>
           <toolchains>
             <jdk>
-              <version>17</version>
+              <version>[17,18)</version>
             </jdk>
           </toolchains>
         </configuration>
diff --git a/src/it/github-470/invoker.properties 
b/src/it/github-470-1.8/invoker.properties
similarity index 95%
copy from src/it/github-470/invoker.properties
copy to src/it/github-470-1.8/invoker.properties
index 97785e8..0a3173a 100644
--- a/src/it/github-470/invoker.properties
+++ b/src/it/github-470-1.8/invoker.properties
@@ -15,6 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-invoker.toolchain.jdk.version = 17
+invoker.toolchain.jdk.version = [1.8,9)
 
 invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1
diff --git a/src/it/github-470/pom.xml b/src/it/github-470-1.8/pom.xml
similarity index 98%
copy from src/it/github-470/pom.xml
copy to src/it/github-470-1.8/pom.xml
index 4220c39..48d589d 100644
--- a/src/it/github-470/pom.xml
+++ b/src/it/github-470-1.8/pom.xml
@@ -44,7 +44,7 @@
         <configuration>
           <toolchains>
             <jdk>
-              <version>17</version>
+              <version>[1.8,9)</version>
             </jdk>
           </toolchains>
         </configuration>
diff --git a/src/it/github-470-1.8/src/main/java/myproject/HelloWorld.java 
b/src/it/github-470-1.8/src/main/java/myproject/HelloWorld.java
new file mode 100644
index 0000000..fd0ad83
--- /dev/null
+++ b/src/it/github-470-1.8/src/main/java/myproject/HelloWorld.java
@@ -0,0 +1,36 @@
+package myproject;
+
+/*
+ * 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.
+ */
+
+/**
+ * The classic Hello World App.
+ */
+public class HelloWorld {
+
+  /**
+     * Main method.
+     *
+     * @param args Not used
+     */
+    public static void main( String[] args )
+    {
+        System.out.println( "Hi!" );
+    } 
+}
\ No newline at end of file
diff --git a/src/it/github-470-1.8/verify.groovy 
b/src/it/github-470-1.8/verify.groovy
new file mode 100644
index 0000000..d4e394e
--- /dev/null
+++ b/src/it/github-470-1.8/verify.groovy
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+import java.util.jar.JarFile
+
+def mrjar = new JarFile(new File(basedir, 'target/gh-470-1.0-SNAPSHOT.jar'))
+def manifest = mrjar.manifest.mainAttributes
+
+assert manifest.getValue("Build-Jdk-Spec") == "1.8"
+assert manifest.getValue("Build-Tool-Jdk-Spec") == 
System.getProperty("java.specification.version")
diff --git a/src/it/github-470/invoker.properties 
b/src/it/github-470/invoker.properties
index 97785e8..230bd22 100644
--- a/src/it/github-470/invoker.properties
+++ b/src/it/github-470/invoker.properties
@@ -15,6 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-invoker.toolchain.jdk.version = 17
+invoker.toolchain.jdk.version = [17,18)
 
 invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1
diff --git a/src/it/github-470/pom.xml b/src/it/github-470/pom.xml
index 4220c39..b3d67cb 100644
--- a/src/it/github-470/pom.xml
+++ b/src/it/github-470/pom.xml
@@ -44,7 +44,7 @@
         <configuration>
           <toolchains>
             <jdk>
-              <version>17</version>
+              <version>[17,18)</version>
             </jdk>
           </toolchains>
         </configuration>
diff --git 
a/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java 
b/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java
index 5d01d41..77dc3df 100644
--- a/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java
+++ b/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java
@@ -28,7 +28,9 @@ import java.io.InputStreamReader;
 import java.io.UncheckedIOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
@@ -73,8 +75,8 @@ class ToolchainsJdkSpecification {
             ProcessBuilder processBuilder = new 
ProcessBuilder(path.toString(), "-version");
             processBuilder.redirectErrorStream(false);
             Process process = processBuilder.start();
-            String stdout = readOutput(process.getInputStream()).trim();
-            String stderr = readOutput(process.getErrorStream()).trim();
+            List<String> stdout = readOutput(process.getInputStream());
+            List<String> stderr = readOutput(process.getErrorStream());
             process.waitFor();
 
             String version = tryParseVersion(stdout);
@@ -100,28 +102,30 @@ class ToolchainsJdkSpecification {
         return null;
     }
 
-    private String tryParseVersion(String version) {
-        if (version.startsWith("javac ")) {
-            version = version.substring(6);
-            if (version.startsWith("1.")) {
-                version = version.substring(0, 3);
-            } else {
-                version = version.substring(0, 2);
+    private String tryParseVersion(List<String> versions) {
+        for (String version : versions) {
+            if (version.startsWith("javac ")) {
+                version = version.substring(6);
+                if (version.startsWith("1.")) {
+                    version = version.substring(0, 3);
+                } else {
+                    version = version.substring(0, 2);
+                }
+                return version;
             }
-            return version;
         }
         return null;
     }
 
-    private String readOutput(InputStream inputstream) throws IOException {
+    private List<String> readOutput(InputStream inputstream) throws 
IOException {
         BufferedReader br = new BufferedReader(new 
InputStreamReader(inputstream));
 
-        StringBuilder output = new StringBuilder();
+        List<String> result = new ArrayList<>();
         String line;
         while ((line = br.readLine()) != null) {
-            output.append(line + System.lineSeparator());
+            result.add(line);
         }
 
-        return output.toString();
+        return result;
     }
 }

Reply via email to