Skip to content

Commit d64b719

Browse files
committed
Merge pull request #18732 from nosan
* pr/18732: Polish "Add toolchains support for Spring Boot Maven Plugin" Add toolchains support for Spring Boot Maven Plugin Closes gh-18732
2 parents 08ac72f + e7f45d8 commit d64b719

File tree

10 files changed

+130
-4
lines changed

10 files changed

+130
-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
@@ -235,6 +235,7 @@
235235
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
236236
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
237237
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
238+
<maven-toolchains-plugin.version>3.0.0</maven-toolchains-plugin.version>
238239
<maven-war-plugin.version>3.2.3</maven-war-plugin.version>
239240
<versions-maven-plugin.version>2.7</versions-maven-plugin.version>
240241
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
@@ -3332,6 +3333,11 @@
33323333
<artifactId>maven-surefire-plugin</artifactId>
33333334
<version>${maven-surefire-plugin.version}</version>
33343335
</plugin>
3336+
<plugin>
3337+
<groupId>org.apache.maven.plugins</groupId>
3338+
<artifactId>maven-toolchains-plugin</artifactId>
3339+
<version>${maven-toolchains-plugin.version}</version>
3340+
</plugin>
33353341
<plugin>
33363342
<groupId>org.apache.maven.plugins</groupId>
33373343
<artifactId>maven-war-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
<version>42</version>
30+
<vendor>test</vendor>
31+
</jdk>
32+
</toolchains>
33+
</configuration>
34+
</plugin>
35+
<plugin>
36+
<groupId>@project.groupId@</groupId>
37+
<artifactId>@project.artifactId@</artifactId>
38+
<version>@project.version@</version>
39+
<executions>
40+
<execution>
41+
<phase>package</phase>
42+
<goals>
43+
<goal>run</goal>
44+
</goals>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<toolchains>
2+
<toolchain>
3+
<type>jdk</type>
4+
<provides>
5+
<version>42</version>
6+
<vendor>test</vendor>
7+
</provides>
8+
<configuration>
9+
<jdkHome>jdkHome</jdkHome>
10+
</configuration>
11+
</toolchain>
12+
</toolchains>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def file = new File(basedir, "build.log")
2+
return file.text.contains("The Maven Toolchains is awesome!")

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
/**
@@ -64,6 +69,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
6469
@Parameter(defaultValue = "${project}", readonly = true, required = true)
6570
private MavenProject project;
6671

72+
/**
73+
* The current Maven session. This is used for toolchain manager API calls.
74+
* @since 2.3.0
75+
*/
76+
@Parameter(defaultValue = "${session}", readonly = true)
77+
private MavenSession session;
78+
79+
/**
80+
* The toolchain manager to use to locate a custom JDK.
81+
* @since 2.3.0
82+
*/
83+
@Component
84+
private ToolchainManager toolchainManager;
85+
6786
/**
6887
* Add maven resources to the classpath directly, this allows live in-place editing of
6988
* resources. Duplicate resources are removed from {@code target/classes} to prevent
@@ -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)