Configure buildpack to use target Java version
With this commit, the Maven `spring-boot:build-image` goal and the Gradle `bootBuildImage` task will configure the OpenJDK buildpack to use the same JRE version as the project's target version, provided the buildpack Java version is not explicitly set in the build configuration. Fixes gh-20172
This commit is contained in:
@@ -88,8 +88,8 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||
"Assembles an executable jar archive containing the main classes and their dependencies.");
|
||||
bootJar.setGroup(BasePlugin.BUILD_GROUP);
|
||||
bootJar.classpath((Callable<FileCollection>) () -> {
|
||||
JavaPluginConvention convention = project.getConvention().getPlugin(JavaPluginConvention.class);
|
||||
SourceSet mainSourceSet = convention.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||
SourceSet mainSourceSet = javaPluginConvention(project).getSourceSets()
|
||||
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||
return mainSourceSet.getRuntimeClasspath();
|
||||
});
|
||||
bootJar.conventionMapping("mainClassName", new MainClassConvention(project, bootJar::getClasspath));
|
||||
@@ -102,6 +102,7 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||
buildImage.setDescription("Builds an OCI image of the application using the output of the bootJar task");
|
||||
buildImage.setGroup(BasePlugin.BUILD_GROUP);
|
||||
buildImage.getJar().set(bootJar.getArchiveFile());
|
||||
buildImage.getTargetJavaVersion().set(javaPluginConvention(project).getTargetCompatibility());
|
||||
}
|
||||
|
||||
private void configureArtifactPublication(BootJar bootJar) {
|
||||
@@ -110,11 +111,11 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||
}
|
||||
|
||||
private void configureBootRunTask(Project project) {
|
||||
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
|
||||
BootRun run = project.getTasks().create("bootRun", BootRun.class);
|
||||
run.setDescription("Runs this project as a Spring Boot application.");
|
||||
run.setGroup(ApplicationPlugin.APPLICATION_GROUP);
|
||||
run.classpath(javaConvention.getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath());
|
||||
run.classpath(javaPluginConvention(project).getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME)
|
||||
.getRuntimeClasspath());
|
||||
run.getConventionMapping().map("jvmArgs", () -> {
|
||||
if (project.hasProperty("applicationDefaultJvmArgs")) {
|
||||
return project.property("applicationDefaultJvmArgs");
|
||||
@@ -124,6 +125,10 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||
run.conventionMapping("main", new MainClassConvention(project, run::getClasspath));
|
||||
}
|
||||
|
||||
private JavaPluginConvention javaPluginConvention(Project project) {
|
||||
return project.getConvention().getPlugin(JavaPluginConvention.class);
|
||||
}
|
||||
|
||||
private void configureUtf8Encoding(Project project) {
|
||||
project.afterEvaluate((evaluated) -> evaluated.getTasks().withType(JavaCompile.class, (compile) -> {
|
||||
if (compile.getOptions().getEncoding() == null) {
|
||||
|
||||
@@ -21,9 +21,11 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gradle.api.DefaultTask;
|
||||
import org.gradle.api.JavaVersion;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.gradle.api.file.RegularFileProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.Optional;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
@@ -48,8 +50,12 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class BootBuildImage extends DefaultTask {
|
||||
|
||||
private static final String OPENJDK_BUILDPACK_JAVA_VERSION_KEY = "BP_JAVA_VERSION";
|
||||
|
||||
private RegularFileProperty jar;
|
||||
|
||||
private Property<JavaVersion> targetJavaVersion;
|
||||
|
||||
private String imageName;
|
||||
|
||||
private String builder;
|
||||
@@ -62,6 +68,7 @@ public class BootBuildImage extends DefaultTask {
|
||||
|
||||
public BootBuildImage() {
|
||||
this.jar = getProject().getObjects().fileProperty();
|
||||
this.targetJavaVersion = getProject().getObjects().property(JavaVersion.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,6 +80,17 @@ public class BootBuildImage extends DefaultTask {
|
||||
return this.jar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the target Java version of the project (e.g. as provided by the
|
||||
* {@code targetCompatibility} build property).
|
||||
* @return the target Java version
|
||||
*/
|
||||
@Input
|
||||
@Optional
|
||||
public Property<JavaVersion> getTargetJavaVersion() {
|
||||
return this.targetJavaVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the image that will be built. When {@code null}, the name will
|
||||
* be derived from the {@link Project Project's} {@link Project#getName() name} and
|
||||
@@ -189,9 +207,8 @@ public class BootBuildImage extends DefaultTask {
|
||||
}
|
||||
|
||||
BuildRequest createRequest() {
|
||||
BuildRequest request = customize(BuildRequest.of(determineImageReference(),
|
||||
return customize(BuildRequest.of(determineImageReference(),
|
||||
(owner) -> new ZipFileTarArchive(this.jar.get().getAsFile(), owner)));
|
||||
return request;
|
||||
}
|
||||
|
||||
private ImageReference determineImageReference() {
|
||||
@@ -207,19 +224,41 @@ public class BootBuildImage extends DefaultTask {
|
||||
}
|
||||
|
||||
private BuildRequest customize(BuildRequest request) {
|
||||
if (StringUtils.hasText(this.builder)) {
|
||||
request = request.withBuilder(ImageReference.of(this.builder));
|
||||
}
|
||||
if (this.environment != null && !this.environment.isEmpty()) {
|
||||
request = request.withEnv(this.environment);
|
||||
}
|
||||
String springBootVersion = VersionExtractor.forClass(BootBuildImage.class);
|
||||
if (StringUtils.hasText(springBootVersion)) {
|
||||
request = request.withCreator(Creator.withVersion(springBootVersion));
|
||||
}
|
||||
request = customizeBuilder(request);
|
||||
request = customizeEnvironment(request);
|
||||
request = customizeCreator(request);
|
||||
request = request.withCleanCache(this.cleanCache);
|
||||
request = request.withVerboseLogging(this.verboseLogging);
|
||||
return request;
|
||||
}
|
||||
|
||||
private BuildRequest customizeBuilder(BuildRequest request) {
|
||||
if (StringUtils.hasText(this.builder)) {
|
||||
return request.withBuilder(ImageReference.of(this.builder));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private BuildRequest customizeEnvironment(BuildRequest request) {
|
||||
if (this.environment != null && !this.environment.isEmpty()) {
|
||||
request = request.withEnv(this.environment);
|
||||
}
|
||||
if (this.targetJavaVersion.isPresent() && !request.getEnv().containsKey(OPENJDK_BUILDPACK_JAVA_VERSION_KEY)) {
|
||||
request = request.withEnv(OPENJDK_BUILDPACK_JAVA_VERSION_KEY, translateTargetJavaVersion());
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private BuildRequest customizeCreator(BuildRequest request) {
|
||||
String springBootVersion = VersionExtractor.forClass(BootBuildImage.class);
|
||||
if (StringUtils.hasText(springBootVersion)) {
|
||||
return request.withCreator(Creator.withVersion(springBootVersion));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private String translateTargetJavaVersion() {
|
||||
return this.targetJavaVersion.get().getMajorVersion() + ".*";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gradle.api.JavaVersion;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.testfixtures.ProjectBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -128,6 +129,27 @@ class BootBuildImageTests {
|
||||
.hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJavaVersionIsSetInEnvironmentItIsIncludedInTheRequest() {
|
||||
this.buildImage.environment("BP_JAVA_VERSION", "from-env");
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_1_8);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("BP_JAVA_VERSION", "from-env").hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenTargetCompatibilityIsSetThenJavaVersionIsIncludedInTheRequest() {
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_1_8);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("BP_JAVA_VERSION", "8.*").hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenTargetCompatibilityIsSetThenJavaVersionIsAddedToEnvironment() {
|
||||
this.buildImage.environment("ALPHA", "a");
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_11);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("ALPHA", "a")
|
||||
.containsEntry("BP_JAVA_VERSION", "11.*").hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingDefaultConfigurationThenRequestHasVerboseLoggingDisabled() {
|
||||
assertThat(this.buildImage.createRequest().isVerboseLogging()).isFalse();
|
||||
|
||||
Reference in New Issue
Block a user