diff --git a/src/it/github-470/invoker.properties b/src/it/github-470/invoker.properties new file mode 100644 index 00000000..97785e88 --- /dev/null +++ b/src/it/github-470/invoker.properties @@ -0,0 +1,20 @@ +# 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. + +invoker.toolchain.jdk.version = 17 + +invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1 diff --git a/src/it/github-470/pom.xml b/src/it/github-470/pom.xml new file mode 100644 index 00000000..4220c399 --- /dev/null +++ b/src/it/github-470/pom.xml @@ -0,0 +1,60 @@ + + + + 4.0.0 + org.apache.maven.plugins + gh-470 + gh-470 + jar + 1.0-SNAPSHOT + + jar plugin it + + + + + + org.apache.maven.plugins + maven-toolchains-plugin + 3.1.0 + + + + toolchain + + + + + + + 17 + + + + + + + org.apache.maven.plugins + maven-jar-plugin + @project.version@ + + + + diff --git a/src/it/github-470/src/main/java/myproject/HelloWorld.java b/src/it/github-470/src/main/java/myproject/HelloWorld.java new file mode 100644 index 00000000..fd0ad83c --- /dev/null +++ b/src/it/github-470/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/verify.groovy b/src/it/github-470/verify.groovy new file mode 100644 index 00000000..78efc30d --- /dev/null +++ b/src/it/github-470/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") == "17" +assert manifest.getValue("Build-Tool-Jdk-Spec") == System.getProperty("java.specification.version") 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 ef992ab0..5d01d414 100644 --- a/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java +++ b/src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java @@ -71,22 +71,26 @@ private Path getCanonicalPath(Path path) { private String getSpecForPath(Path path) { try { ProcessBuilder processBuilder = new ProcessBuilder(path.toString(), "-version"); - processBuilder.redirectErrorStream(true); + processBuilder.redirectErrorStream(false); Process process = processBuilder.start(); - String version = readOutput(process.getInputStream()).trim(); + String stdout = readOutput(process.getInputStream()).trim(); + String stderr = readOutput(process.getErrorStream()).trim(); process.waitFor(); - 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; - } else { - logger.warn("Unrecognized output from {}: {}", processBuilder.command(), version); + String version = tryParseVersion(stdout); + if (version == null) { + version = tryParseVersion(stderr); + } + + if (version == null) { + logger.warn( + "Unrecognized output from {}: stdout: {}, stderr: {}", + processBuilder.command(), + stdout, + stderr); } + + return version; } catch (IndexOutOfBoundsException | IOException e) { logger.warn("Failed to execute: {} - {}", path, e.getMessage()); } catch (InterruptedException e) { @@ -96,6 +100,19 @@ private String getSpecForPath(Path path) { 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); + } + return version; + } + return null; + } + private String readOutput(InputStream inputstream) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));