Skip to content

Commit

Permalink
[GR-22589] Use Maven Toolchain in native-image-maven-plugin.
Browse files Browse the repository at this point in the history
PullRequest: graal/5979
  • Loading branch information
olpaw committed Apr 21, 2020
2 parents 9abecf7 + cd933db commit c55645f
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.ConfigurationContainer;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.ToolchainManager;
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.logging.AbstractLogger;
import org.codehaus.plexus.logging.Logger;
Expand Down Expand Up @@ -88,6 +93,12 @@ public class NativeImageMojo extends AbstractMojo {
@Parameter(property = "skip", defaultValue = "false")//
private boolean skip;

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

@Component
private ToolchainManager toolchainManager;

private Logger tarGzLogger = new AbstractLogger(Logger.LEVEL_WARN, "NativeImageMojo.tarGzLogger") {
@Override
public void debug(String message, Throwable throwable) {
Expand Down Expand Up @@ -302,8 +313,12 @@ private void addClasspath(Artifact artifact) throws MojoExecutionException {
classpath.add(jarFilePath);
}

private static Path getMojoJavaHome() {
return Paths.get(System.getProperty("java.home"));
private Path getMojoJavaHome() {
return Paths.get(Optional.ofNullable(toolchainManager)
.map(tm -> tm.getToolchainFromBuildContext("jdk", session))
.filter(DefaultJavaToolChain.class::isInstance).map(DefaultJavaToolChain.class::cast)
.map(DefaultJavaToolChain::getJavaHome)
.orElse(System.getProperty("java.home")));
}

private Path getWorkingDirectory() {
Expand Down

0 comments on commit c55645f

Please sign in to comment.