diff --git a/spring-boot-project/spring-boot-dependencies/pom.xml b/spring-boot-project/spring-boot-dependencies/pom.xml index 93fb21a48b7..df4de379e26 100644 --- a/spring-boot-project/spring-boot-dependencies/pom.xml +++ b/spring-boot-project/spring-boot-dependencies/pom.xml @@ -239,6 +239,7 @@ 2.7 1.0.2 1.1.0 + 3.0.0 @@ -3337,6 +3338,11 @@ maven-war-plugin ${maven-war-plugin.version} + + org.apache.maven.plugins + maven-toolchains-plugin + ${maven-toolchains-plugin.version} + org.codehaus.mojo build-helper-maven-plugin diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/invoker.properties b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/invoker.properties new file mode 100644 index 00000000000..12b66a0fa59 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals=clean verify -t toolchains.xml +invoker.os.family=!windows diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/jdkHome/bin/java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/jdkHome/bin/java new file mode 100755 index 00000000000..41f7d6efb02 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/jdkHome/bin/java @@ -0,0 +1,2 @@ +#!/bin/bash +echo 'The Maven Toolchains is awesome!' diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/pom.xml new file mode 100644 index 00000000000..ca93b1957b5 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + org.springframework.boot.maven.it + run-toolchains + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + @java.version@ + @java.version@ + + + + + org.apache.maven.plugins + maven-toolchains-plugin + @maven-toolchains-plugin.version@ + + + + toolchain + + + + + + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + package + + run + + + + + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/src/main/java/org/test/SampleApplication.java new file mode 100644 index 00000000000..4d4b615ae2b --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.test; + +public class SampleApplication { + + public static void main(String[] args) { + throw new IllegalStateException("Should not be called!"); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/toolchains.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/toolchains.xml new file mode 100644 index 00000000000..386ea6262cb --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/toolchains.xml @@ -0,0 +1,8 @@ + + + jdk + + jdkHome + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/verify.groovy new file mode 100644 index 00000000000..bb7fe7aff2c --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/verify.groovy @@ -0,0 +1,3 @@ +def file = new File(basedir, "build.log") +return file.text.contains("The Maven Toolchains is awesome!") + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java index b76a6e95883..c3860e5f797 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java @@ -30,15 +30,20 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Resource; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; +import org.apache.maven.toolchain.Toolchain; +import org.apache.maven.toolchain.ToolchainManager; import org.springframework.boot.loader.tools.FileUtils; +import org.springframework.boot.loader.tools.JavaExecutable; import org.springframework.boot.loader.tools.MainClassFinder; /** @@ -204,6 +209,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { @Parameter(property = "spring-boot.run.skip", defaultValue = "false") private boolean skip; + /** + * The Maven Session Object. + * @since 2.2.1 + */ + @Parameter(defaultValue = "${session}", readonly = true) + private MavenSession session; + + /** + * The toolchain manager to use. + * @since 2.2.1 + */ + @Component + private ToolchainManager toolchainManager; + @Override public void execute() throws MojoExecutionException, MojoFailureException { if (this.skip) { @@ -325,6 +344,16 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { return runArguments; } + /** + * Provides access to the java binary executable, regardless of OS. + * @return the java executable + **/ + protected String getJavaExecutable() { + Toolchain toolchain = this.toolchainManager.getToolchainFromBuildContext("jdk", this.session); + String javaExecutable = (toolchain != null) ? toolchain.findTool("java") : null; + return (javaExecutable != null) ? javaExecutable : new JavaExecutable().toString(); + } + /** * Resolve the environment variables to use. * @return an {@link EnvVariables} defining the environment variables diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java index 0b398faeee0..7b97246729d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java @@ -30,7 +30,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; -import org.springframework.boot.loader.tools.JavaExecutable; import org.springframework.boot.loader.tools.RunProcess; /** @@ -111,7 +110,7 @@ public class RunMojo extends AbstractRunMojo { private int forkJvm(File workingDirectory, List args, Map environmentVariables) throws MojoExecutionException { try { - RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString()); + RunProcess runProcess = new RunProcess(workingDirectory, getJavaExecutable()); Runtime.getRuntime().addShutdownHook(new Thread(new RunProcessKiller(runProcess))); return runProcess.run(true, args, environmentVariables); } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java index f1cbfa1bddd..0ae146fd420 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java @@ -37,7 +37,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; -import org.springframework.boot.loader.tools.JavaExecutable; import org.springframework.boot.loader.tools.RunProcess; /** @@ -104,7 +103,7 @@ public class StartMojo extends AbstractRunMojo { private RunProcess runProcess(File workingDirectory, List args, Map environmentVariables) throws MojoExecutionException { try { - RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString()); + RunProcess runProcess = new RunProcess(workingDirectory, getJavaExecutable()); runProcess.run(false, args, environmentVariables); return runProcess; }