Skip to content

Commit 340249c

Browse files
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
1 parent 06a6245 commit 340249c

File tree

9 files changed

+164
-18
lines changed

9 files changed

+164
-18
lines changed

src/it/MJAR-62-toolchain/invoker.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
invoker.toolchain.jdk.version = 17
18+
invoker.toolchain.jdk.version = [17,18)

src/it/MJAR-62-toolchain/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<configuration>
4545
<toolchains>
4646
<jdk>
47-
<version>17</version>
47+
<version>[17,18)</version>
4848
</jdk>
4949
</toolchains>
5050
</configuration>
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 = [1.8,9)
19+
20+
invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1

src/it/github-470-1.8/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>[1.8,9)</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+
}
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") == "1.8"
26+
assert manifest.getValue("Build-Tool-Jdk-Spec") == System.getProperty("java.specification.version")

src/it/github-470/invoker.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
invoker.toolchain.jdk.version = 17
18+
invoker.toolchain.jdk.version = [17,18)
1919

2020
invoker.environmentVariables.JAVA_TOOL_OPTIONS = -XX:ActiveProcessorCount=1

src/it/github-470/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<configuration>
4545
<toolchains>
4646
<jdk>
47-
<version>17</version>
47+
<version>[17,18)</version>
4848
</jdk>
4949
</toolchains>
5050
</configuration>

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import java.io.UncheckedIOException;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31+
import java.util.ArrayList;
3132
import java.util.HashMap;
33+
import java.util.List;
3234
import java.util.Map;
3335
import java.util.Optional;
3436

@@ -73,8 +75,8 @@ private String getSpecForPath(Path path) {
7375
ProcessBuilder processBuilder = new ProcessBuilder(path.toString(), "-version");
7476
processBuilder.redirectErrorStream(false);
7577
Process process = processBuilder.start();
76-
String stdout = readOutput(process.getInputStream()).trim();
77-
String stderr = readOutput(process.getErrorStream()).trim();
78+
List<String> stdout = readOutput(process.getInputStream());
79+
List<String> stderr = readOutput(process.getErrorStream());
7880
process.waitFor();
7981

8082
String version = tryParseVersion(stdout);
@@ -100,28 +102,30 @@ private String getSpecForPath(Path path) {
100102
return null;
101103
}
102104

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);
105+
private String tryParseVersion(List<String> versions) {
106+
for (String version : versions) {
107+
if (version.startsWith("javac ")) {
108+
version = version.substring(6);
109+
if (version.startsWith("1.")) {
110+
version = version.substring(0, 3);
111+
} else {
112+
version = version.substring(0, 2);
113+
}
114+
return version;
110115
}
111-
return version;
112116
}
113117
return null;
114118
}
115119

116-
private String readOutput(InputStream inputstream) throws IOException {
120+
private List<String> readOutput(InputStream inputstream) throws IOException {
117121
BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
118122

119-
StringBuilder output = new StringBuilder();
123+
List<String> result = new ArrayList<>();
120124
String line;
121125
while ((line = br.readLine()) != null) {
122-
output.append(line + System.lineSeparator());
126+
result.add(line);
123127
}
124128

125-
return output.toString();
129+
return result;
126130
}
127131
}

0 commit comments

Comments
 (0)