From c81eb9b7c5ea95ea2bcfd88058270d7123745a48 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 24 Oct 2019 20:36:24 +0300 Subject: [PATCH] Provide toolchains support for Spring Boot Maven Plugin gh-18686 --- .../spring-boot-dependencies/pom.xml | 6 +++ .../src/it/run-toolchains/invoker.properties | 2 + .../src/it/run-toolchains/jdkHome/bin/java | 2 + .../src/it/run-toolchains/pom.xml | 47 +++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 25 ++++++++++ .../src/it/run-toolchains/toolchains.xml | 8 ++++ .../src/it/run-toolchains/verify.groovy | 3 ++ .../boot/maven/AbstractRunMojo.java | 29 ++++++++++++ .../springframework/boot/maven/RunMojo.java | 3 +- .../springframework/boot/maven/StartMojo.java | 3 +- 10 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/invoker.properties create mode 100755 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/jdkHome/bin/java create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/toolchains.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-toolchains/verify.groovy diff --git a/spring-boot-project/spring-boot-dependencies/pom.xml b/spring-boot-project/spring-boot-dependencies/pom.xml index 48578a9990c7..629fea58d593 100644 --- a/spring-boot-project/spring-boot-dependencies/pom.xml +++ b/spring-boot-project/spring-boot-dependencies/pom.xml @@ -240,6 +240,7 @@ 2.7 1.0.2 1.1.0 + 3.0.0 @@ -3349,6 +3350,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 000000000000..12b66a0fa599 --- /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 000000000000..41f7d6efb02f --- /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 000000000000..ca93b1957b58 --- /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 000000000000..4d4b615ae2b8 --- /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 000000000000..386ea6262cbb --- /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 000000000000..bb7fe7aff2c5 --- /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 013e4accc533..a7e4013d31d7 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.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; /** @@ -196,6 +201,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) { @@ -316,6 +335,16 @@ protected RunArguments resolveApplicationArguments() { 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 510ae049a9a1..2d3d53b431ed 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.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 @@ protected void runWithForkedJvm(File workingDirectory, List args, Map 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 240d9d41a7cb..5c65310bbe37 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.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 @@ protected void runWithForkedJvm(File workingDirectory, List args, Map 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; }