Add Build-Jdk-Spec to jar and war manifest when building with Gradle
This commit adds a `Build-Jdk-Spec` attribute to the manifest in a jar or war file built with the Spring Boot Gradle plugin. This aligns the Gradle plugin's behavior with the default Maven plugin behavior. This removes the need to set a `BP_JVM_VERSION` environment variable when invoking Cloud Native Buildpacks, as the Paketo buildpacks will honor `Build-Jdk-Spec` in a jar or war manifest to determine the default JVM version. Fixes gh-32829
This commit is contained in:
@@ -478,6 +478,15 @@ abstract class AbstractBootArchiveIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void javaVersionIsSetInManifest() throws IOException {
|
||||
BuildResult result = this.gradleBuild.build(this.taskName);
|
||||
assertThat(result.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
try (JarFile jarFile = new JarFile(new File(this.gradleBuild.getProjectDir(), "build/libs").listFiles()[0])) {
|
||||
assertThat(jarFile.getManifest().getMainAttributes().getValue("Build-Jdk-Spec")).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private void copyMainClassApplication() throws IOException {
|
||||
copyApplication("main");
|
||||
}
|
||||
|
||||
@@ -73,7 +73,6 @@ class BootBuildImageIntegrationTests {
|
||||
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
|
||||
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
|
||||
assertThat(result.getOutput()).contains("env: BP_JVM_VERSION=8.*");
|
||||
assertThat(result.getOutput()).contains("Network status: HTTP/2 200");
|
||||
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
|
||||
removeImages(projectName);
|
||||
@@ -88,7 +87,6 @@ class BootBuildImageIntegrationTests {
|
||||
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
|
||||
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
|
||||
assertThat(result.getOutput()).contains("env: BP_JVM_VERSION=8.*");
|
||||
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
|
||||
File buildLibs = new File(this.gradleBuild.getProjectDir(), "build/libs");
|
||||
assertThat(buildLibs.listFiles())
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gradle.api.JavaVersion;
|
||||
import org.gradle.api.Project;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -139,27 +138,6 @@ class BootBuildImageTests {
|
||||
.hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJavaVersionIsSetInEnvironmentItIsIncludedInTheRequest() {
|
||||
this.buildImage.getEnvironment().put("BP_JVM_VERSION", "from-env");
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_1_8);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("BP_JVM_VERSION", "from-env").hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenTargetCompatibilityIsSetThenJavaVersionIsIncludedInTheRequest() {
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_1_8);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("BP_JVM_VERSION", "8.*").hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenTargetCompatibilityIsSetThenJavaVersionIsAddedToEnvironment() {
|
||||
this.buildImage.getEnvironment().put("ALPHA", "a");
|
||||
this.buildImage.getTargetJavaVersion().set(JavaVersion.VERSION_11);
|
||||
assertThat(this.buildImage.createRequest().getEnv()).containsEntry("ALPHA", "a")
|
||||
.containsEntry("BP_JVM_VERSION", "11.*").hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingDefaultConfigurationThenRequestHasVerboseLoggingDisabled() {
|
||||
assertThat(this.buildImage.createRequest().isVerboseLogging()).isFalse();
|
||||
|
||||
@@ -22,7 +22,9 @@ import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.JavaVersion;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
@@ -45,6 +47,11 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
|
||||
"BOOT-INF/");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.getTask().getTargetJavaVersion().set(JavaVersion.VERSION_17);
|
||||
}
|
||||
|
||||
@Test
|
||||
void contentCanBeAddedToBootInfUsingCopySpecFromGetter() throws IOException {
|
||||
BootJar bootJar = getTask();
|
||||
@@ -194,6 +201,14 @@ class BootJarTests extends AbstractBootArchiveTests<BootJar> {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaVersionIsWrittenToManifest() throws IOException {
|
||||
try (JarFile jarFile = new JarFile(createPopulatedJar())) {
|
||||
assertThat(jarFile.getManifest().getMainAttributes().getValue("Build-Jdk-Spec"))
|
||||
.isEqualTo(JavaVersion.VERSION_17.getMajorVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void applyLayered(Action<LayeredSpec> action) {
|
||||
getTask().layered(action);
|
||||
|
||||
@@ -21,7 +21,9 @@ import java.io.IOException;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.JavaVersion;
|
||||
import org.gradle.api.artifacts.Configuration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
@@ -42,6 +44,11 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
|
||||
"WEB-INF/");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.getTask().getTargetJavaVersion().set(JavaVersion.VERSION_17);
|
||||
}
|
||||
|
||||
@Test
|
||||
void providedClasspathJarsArePackagedInWebInfLibProvided() throws IOException {
|
||||
getTask().getMainClass().set("com.example.Main");
|
||||
@@ -137,6 +144,14 @@ class BootWarTests extends AbstractBootArchiveTests<BootWar> {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void javaVersionIsWrittenToManifest() throws IOException {
|
||||
try (JarFile jarFile = new JarFile(createPopulatedJar())) {
|
||||
assertThat(jarFile.getManifest().getMainAttributes().getValue("Build-Jdk-Spec"))
|
||||
.isEqualTo(JavaVersion.VERSION_17.getMajorVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeTask() {
|
||||
getTask().copy();
|
||||
|
||||
Reference in New Issue
Block a user