Skip to content

Commit

Permalink
integrate maven-toolchain support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Doherty committed Jan 7, 2025
1 parent 22629d3 commit ec0b20c
Showing 1 changed file with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.logging.Logger;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -42,6 +43,8 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
Expand Down Expand Up @@ -131,6 +134,15 @@ abstract class AbstractJaxwsMojo extends AbstractMojo {
@Parameter
private File executable;

@Component
private ToolchainManager toolchainManager;

@Parameter(defaultValue = "false")
private boolean useJdkToolchainExecutable;

@Parameter(defaultValue = "${session}", readonly = true, required = true)
protected MavenSession session;

/**
* The entry point to Aether, i.e. the component doing all the work.
*
Expand Down Expand Up @@ -340,7 +352,7 @@ protected void exec(List<String> args) throws MojoExecutionException {
throw new MojoExecutionException("Cannot execute: " + executable.getAbsolutePath());
}
} else {
cmd.setExecutable(new File(new File(System.getProperty("java.home"), "bin"), getJavaExec()).getAbsolutePath());
cmd.setExecutable(new File(new File(getJavaHome(), "bin"), getJavaExec()).getAbsolutePath());
// add additional JVM options
if (vmArgs != null) {
for (String arg : vmArgs) {
Expand Down Expand Up @@ -441,10 +453,12 @@ private String[] getCP() throws DependencyResolutionException {
throw new RuntimeException(ex);
}
sb.append(File.pathSeparator);

String javaHome = getJavaHome();
//don't forget tools.jar
File toolsJar = new File(System.getProperty("java.home"), "../lib/tools.jar");
File toolsJar = new File(javaHome, "../lib/tools.jar");
if (!toolsJar.exists()) {
toolsJar = new File(System.getProperty("java.home"), "lib/tools.jar");
toolsJar = new File(javaHome, "lib/tools.jar");
}
if (toolsJar.exists()) {
sb.append(toolsJar.getAbsolutePath());
Expand All @@ -458,6 +472,27 @@ private String[] getCP() throws DependencyResolutionException {
private String getJavaExec() {
return isWindows() ? "java.exe" : "java";
}

private String getJavaHome() {
String javaHome = System.getProperty("java.home");
if (getJdkToolchain() != null) {
File javaExecutable = new File(getJdkToolchain().findTool("java"));
javaHome = javaExecutable.getParentFile().getParent();
getLog().info("got java home from maven toolchain: " + javaHome);
}
else {
getLog().info("couldnt get a javahome from maven toolchain, defaulting to the java.home System property : " + javaHome);
}
return javaHome;
}

private Toolchain getJdkToolchain() {
Toolchain tc = null;
if (this.toolchainManager != null) {
tc = this.toolchainManager.getToolchainFromBuildContext("jdk", this.session);
}
return tc;
}

private File createPathFile(String cp) throws IOException {
File f = File.createTempFile("jax-ws-mvn-plugin-cp", ".txt");
Expand Down

0 comments on commit ec0b20c

Please sign in to comment.