Skip to content

Commit 704a35c

Browse files
Ignore stderr when parsing javac version from toolchain (#471)
* Ignore stderr when parsing javac version from toolchain * parse version from stdout, fall back to stderr for older javac --------- Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent 0beb969 commit 704a35c

File tree

5 files changed

+171
-12
lines changed

5 files changed

+171
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.toolchain.jdk.version = 17
19+
20+
invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1

src/it/github-470/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>gh-470</artifactId>
24+
<name>gh-470</name>
25+
<packaging>jar</packaging>
26+
<version>1.0-SNAPSHOT</version>
27+
<organization>
28+
<name>jar plugin it</name>
29+
</organization>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-toolchains-plugin</artifactId>
36+
<version>3.1.0</version>
37+
<executions>
38+
<execution>
39+
<goals>
40+
<goal>toolchain</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
<configuration>
45+
<toolchains>
46+
<jdk>
47+
<version>17</version>
48+
</jdk>
49+
</toolchains>
50+
</configuration>
51+
</plugin>
52+
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-jar-plugin</artifactId>
56+
<version>@project.version@</version>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package myproject;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* The classic Hello World App.
24+
*/
25+
public class HelloWorld {
26+
27+
/**
28+
* Main method.
29+
*
30+
* @param args Not used
31+
*/
32+
public static void main( String[] args )
33+
{
34+
System.out.println( "Hi!" );
35+
}
36+
}

src/it/github-470/verify.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.util.jar.JarFile
21+
22+
def mrjar = new JarFile(new File(basedir, 'target/gh-470-1.0-SNAPSHOT.jar'))
23+
def manifest = mrjar.manifest.mainAttributes
24+
25+
assert manifest.getValue("Build-Jdk-Spec") == "17"
26+
assert manifest.getValue("Build-Tool-Jdk-Spec") == System.getProperty("java.specification.version")

src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,26 @@ private Path getCanonicalPath(Path path) {
7171
private String getSpecForPath(Path path) {
7272
try {
7373
ProcessBuilder processBuilder = new ProcessBuilder(path.toString(), "-version");
74-
processBuilder.redirectErrorStream(true);
74+
processBuilder.redirectErrorStream(false);
7575
Process process = processBuilder.start();
76-
String version = readOutput(process.getInputStream()).trim();
76+
String stdout = readOutput(process.getInputStream()).trim();
77+
String stderr = readOutput(process.getErrorStream()).trim();
7778
process.waitFor();
7879

79-
if (version.startsWith("javac ")) {
80-
version = version.substring(6);
81-
if (version.startsWith("1.")) {
82-
version = version.substring(0, 3);
83-
} else {
84-
version = version.substring(0, 2);
85-
}
86-
return version;
87-
} else {
88-
logger.warn("Unrecognized output from {}: {}", processBuilder.command(), version);
80+
String version = tryParseVersion(stdout);
81+
if (version == null) {
82+
version = tryParseVersion(stderr);
83+
}
84+
85+
if (version == null) {
86+
logger.warn(
87+
"Unrecognized output from {}: stdout: {}, stderr: {}",
88+
processBuilder.command(),
89+
stdout,
90+
stderr);
8991
}
92+
93+
return version;
9094
} catch (IndexOutOfBoundsException | IOException e) {
9195
logger.warn("Failed to execute: {} - {}", path, e.getMessage());
9296
} catch (InterruptedException e) {
@@ -96,6 +100,19 @@ private String getSpecForPath(Path path) {
96100
return null;
97101
}
98102

103+
private String tryParseVersion(String version) {
104+
if (version.startsWith("javac ")) {
105+
version = version.substring(6);
106+
if (version.startsWith("1.")) {
107+
version = version.substring(0, 3);
108+
} else {
109+
version = version.substring(0, 2);
110+
}
111+
return version;
112+
}
113+
return null;
114+
}
115+
99116
private String readOutput(InputStream inputstream) throws IOException {
100117
BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
101118

0 commit comments

Comments
 (0)