-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MJAR-62] Set Build-Jdk according to used toolchain
- Loading branch information
1 parent
adf1c76
commit fa4330f
Showing
9 changed files
with
392 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>mjar-62</artifactId> | ||
<name>MJAR-62</name> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<organization> | ||
<name>jar plugin it</name> | ||
</organization> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-toolchains-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>toolchain</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<toolchains> | ||
<jdk> | ||
<version>17</version> | ||
</jdk> | ||
</toolchains> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>@project.version@</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
36 changes: 36 additions & 0 deletions
36
src/it/MJAR-62-toolchain/src/main/java/myproject/HelloWorld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/mjar-62-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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/org/apache/maven/plugins/jar/ToolchainsJdkSpecification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.plugins.jar; | ||
|
||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.apache.maven.toolchain.Toolchain; | ||
import org.codehaus.plexus.util.cli.CommandLineException; | ||
import org.codehaus.plexus.util.cli.CommandLineUtils; | ||
import org.codehaus.plexus.util.cli.Commandline; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Component provided JDK specification based on toolchains. | ||
*/ | ||
@Named | ||
@Singleton | ||
class ToolchainsJdkSpecification { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(ToolchainsJdkSpecification.class); | ||
|
||
private final Map<Path, String> cache = new HashMap<>(); | ||
|
||
public synchronized Optional<String> getJDKSpecification(Toolchain toolchain) { | ||
Optional<Path> javacPath = getJavacPath(toolchain); | ||
return javacPath.map(path -> cache.computeIfAbsent(path, this::getSpecForPath)); | ||
} | ||
|
||
private Optional<Path> getJavacPath(Toolchain toolchain) { | ||
return Optional.ofNullable(toolchain.findTool("javac")).map(Paths::get).map(this::getCanonicalPath); | ||
} | ||
|
||
private Path getCanonicalPath(Path path) { | ||
try { | ||
return path.toRealPath(); | ||
} catch (IOException e) { | ||
if (path.getParent() != null) { | ||
return getCanonicalPath(path.getParent()).resolve(path.getFileName()); | ||
} else { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
|
||
private String getSpecForPath(Path path) { | ||
try { | ||
Commandline cl = new Commandline(path.toString()); | ||
cl.createArg().setValue("-version"); | ||
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); | ||
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); | ||
CommandLineUtils.executeCommandLine(cl, out, err); | ||
String version = out.getOutput().trim(); | ||
if (version.isEmpty()) { | ||
version = err.getOutput().trim(); | ||
} | ||
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 form " + path + " -version - " + version); | ||
return null; | ||
} | ||
} catch (CommandLineException | IndexOutOfBoundsException e) { | ||
logger.warn("Failed to execute: " + path + " - " + e.getMessage()); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.