Skip to content

Commit eede16f

Browse files
nosansnicoll
authored andcommitted
Add toolchains support for Spring Boot Maven Plugin
See gh-18732
1 parent 08ac72f commit eede16f

File tree

10 files changed

+124
-4
lines changed

10 files changed

+124
-4
lines changed

spring-boot-project/spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
<versions-maven-plugin.version>2.7</versions-maven-plugin.version>
240240
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
241241
<flatten-maven-plugin.version>1.1.0</flatten-maven-plugin.version>
242+
<maven-toolchains-plugin.version>3.0.0</maven-toolchains-plugin.version>
242243
</properties>
243244
<dependencyManagement>
244245
<dependencies>
@@ -3337,6 +3338,11 @@
33373338
<artifactId>maven-war-plugin</artifactId>
33383339
<version>${maven-war-plugin.version}</version>
33393340
</plugin>
3341+
<plugin>
3342+
<groupId>org.apache.maven.plugins</groupId>
3343+
<artifactId>maven-toolchains-plugin</artifactId>
3344+
<version>${maven-toolchains-plugin.version}</version>
3345+
</plugin>
33403346
<plugin>
33413347
<groupId>org.codehaus.mojo</groupId>
33423348
<artifactId>build-helper-maven-plugin</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals=clean verify -t toolchains.xml
2+
invoker.os.family=!windows
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo 'The Maven Toolchains is awesome!'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>run-toolchains</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>@java.version@</maven.compiler.source>
11+
<maven.compiler.target>@java.version@</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-toolchains-plugin</artifactId>
18+
<version>@maven-toolchains-plugin.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>toolchain</goal>
23+
</goals>
24+
</execution>
25+
</executions>
26+
<configuration>
27+
<toolchains>
28+
<jdk/>
29+
</toolchains>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<groupId>@project.groupId@</groupId>
34+
<artifactId>@project.artifactId@</artifactId>
35+
<version>@project.version@</version>
36+
<executions>
37+
<execution>
38+
<phase>package</phase>
39+
<goals>
40+
<goal>run</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
public static void main(String[] args) {
22+
throw new IllegalStateException("Should not be called!");
23+
}
24+
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<toolchains>
2+
<toolchain>
3+
<type>jdk</type>
4+
<configuration>
5+
<jdkHome>jdkHome</jdkHome>
6+
</configuration>
7+
</toolchain>
8+
</toolchains>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def file = new File(basedir, "build.log")
2+
return file.text.contains("The Maven Toolchains is awesome!")
3+

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@
3030
import java.util.stream.Collectors;
3131

3232
import org.apache.maven.artifact.Artifact;
33+
import org.apache.maven.execution.MavenSession;
3334
import org.apache.maven.model.Resource;
3435
import org.apache.maven.plugin.MojoExecutionException;
3536
import org.apache.maven.plugin.MojoFailureException;
37+
import org.apache.maven.plugins.annotations.Component;
3638
import org.apache.maven.plugins.annotations.Parameter;
3739
import org.apache.maven.project.MavenProject;
3840
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
3941
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
42+
import org.apache.maven.toolchain.Toolchain;
43+
import org.apache.maven.toolchain.ToolchainManager;
4044

4145
import org.springframework.boot.loader.tools.FileUtils;
46+
import org.springframework.boot.loader.tools.JavaExecutable;
4247
import org.springframework.boot.loader.tools.MainClassFinder;
4348

4449
/**
@@ -204,6 +209,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
204209
@Parameter(property = "spring-boot.run.skip", defaultValue = "false")
205210
private boolean skip;
206211

212+
/**
213+
* The Maven Session Object.
214+
* @since 2.2.1
215+
*/
216+
@Parameter(defaultValue = "${session}", readonly = true)
217+
private MavenSession session;
218+
219+
/**
220+
* The toolchain manager to use.
221+
* @since 2.2.1
222+
*/
223+
@Component
224+
private ToolchainManager toolchainManager;
225+
207226
@Override
208227
public void execute() throws MojoExecutionException, MojoFailureException {
209228
if (this.skip) {
@@ -325,6 +344,16 @@ protected RunArguments resolveApplicationArguments() {
325344
return runArguments;
326345
}
327346

347+
/**
348+
* Provides access to the java binary executable, regardless of OS.
349+
* @return the java executable
350+
**/
351+
protected String getJavaExecutable() {
352+
Toolchain toolchain = this.toolchainManager.getToolchainFromBuildContext("jdk", this.session);
353+
String javaExecutable = (toolchain != null) ? toolchain.findTool("java") : null;
354+
return (javaExecutable != null) ? javaExecutable : new JavaExecutable().toString();
355+
}
356+
328357
/**
329358
* Resolve the environment variables to use.
330359
* @return an {@link EnvVariables} defining the environment variables

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.maven.plugins.annotations.Parameter;
3131
import org.apache.maven.plugins.annotations.ResolutionScope;
3232

33-
import org.springframework.boot.loader.tools.JavaExecutable;
3433
import org.springframework.boot.loader.tools.RunProcess;
3534

3635
/**
@@ -111,7 +110,7 @@ protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<St
111110
private int forkJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
112111
throws MojoExecutionException {
113112
try {
114-
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
113+
RunProcess runProcess = new RunProcess(workingDirectory, getJavaExecutable());
115114
Runtime.getRuntime().addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
116115
return runProcess.run(true, args, environmentVariables);
117116
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.apache.maven.plugins.annotations.Parameter;
3838
import org.apache.maven.plugins.annotations.ResolutionScope;
3939

40-
import org.springframework.boot.loader.tools.JavaExecutable;
4140
import org.springframework.boot.loader.tools.RunProcess;
4241

4342
/**
@@ -104,7 +103,7 @@ protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<St
104103
private RunProcess runProcess(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
105104
throws MojoExecutionException {
106105
try {
107-
RunProcess runProcess = new RunProcess(workingDirectory, new JavaExecutable().toString());
106+
RunProcess runProcess = new RunProcess(workingDirectory, getJavaExecutable());
108107
runProcess.run(false, args, environmentVariables);
109108
return runProcess;
110109
}

0 commit comments

Comments
 (0)