From c37b9b8a8ed26bd2b4a96313db1497517b7df32a Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 28 Apr 2021 17:08:06 -0500 Subject: [PATCH] Use layout configuration in Maven build-image goal This commit changes the Maven build-image goal to honor the `layout` and `layoutFactory` parameters to ensure that the archive content sent to the builder is the same as is used by the `repackage` goal to build the archive file. Fixes gh-26216 --- .../boot/maven/BuildImageTests.java | 32 +++++++++++++++ .../build-image-zip-packaging/pom.xml | 32 +++++++++++++++ .../main/java/org/test/SampleApplication.java | 28 +++++++++++++ .../boot/maven/BuildImageMojo.java | 40 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/src/main/java/org/test/SampleApplication.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java index 5f6abcd0aaf..e85ac49971a 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java @@ -16,12 +16,16 @@ package org.springframework.boot.maven; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Random; +import java.util.stream.Collectors; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; @@ -262,6 +266,34 @@ public class BuildImageTests extends AbstractArchiveIntegrationTests { }); } + @TestTemplate + void whenBuildImageIsInvokedWithZipPackaging(MavenBuild mavenBuild) { + mavenBuild.project("build-image-zip-packaging"); + mavenBuild.goals("package"); + mavenBuild.prepare(this::writeLongNameResource); + mavenBuild.execute((project) -> { + File jar = new File(project, "target/build-image-zip-packaging-0.0.1.BUILD-SNAPSHOT.jar"); + assertThat(jar).isFile(); + assertThat(buildLog(project)).contains("Building image").contains("paketo-buildpacks/builder") + .contains("docker.io/library/build-image-zip-packaging:0.0.1.BUILD-SNAPSHOT") + .contains("Successfully built image"); + ImageReference imageReference = ImageReference.of(ImageName.of("build-image-zip-packaging"), + "0.0.1.BUILD-SNAPSHOT"); + try (GenericContainer container = new GenericContainer<>(imageReference.toString())) { + container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start(); + container.copyFileFromContainer("/workspace/META-INF/MANIFEST.MF", (inputStream) -> { + String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); + assertThat(text).contains("Main-Class: org.springframework.boot.loader.PropertiesLauncher"); + return null; + }); + } + finally { + removeImage(imageReference); + } + }); + } + @TestTemplate void failsWhenBuilderFails(MavenBuild mavenBuild) { mavenBuild.project("build-image-builder-error").goals("package") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/pom.xml new file mode 100644 index 00000000000..f62888620a6 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + org.springframework.boot.maven.it + build-image-zip-packaging + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + @java.version@ + @java.version@ + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + build-image + + + + + ZIP + + + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/src/main/java/org/test/SampleApplication.java new file mode 100644 index 00000000000..27259ff01ad --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2020 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) throws Exception { + System.out.println("Launched"); + synchronized(args) { + args.wait(); // Prevent exit" + } + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java index 78626d4511e..89be9484be7 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java @@ -48,6 +48,7 @@ import org.springframework.boot.buildpack.platform.io.Owner; import org.springframework.boot.buildpack.platform.io.TarArchive; import org.springframework.boot.loader.tools.EntryWriter; import org.springframework.boot.loader.tools.ImagePackager; +import org.springframework.boot.loader.tools.LayoutFactory; import org.springframework.boot.loader.tools.Libraries; import org.springframework.util.StringUtils; @@ -128,6 +129,45 @@ public class BuildImageMojo extends AbstractPackagerMojo { @Parameter(property = "spring-boot.build-image.runImage", readonly = true) String runImage; + /** + * The type of archive (which corresponds to how the dependencies are laid out inside + * it). Possible values are {@code JAR}, {@code WAR}, {@code ZIP}, {@code DIR}, + * {@code NONE}. Defaults to a guess based on the archive type. + * @since 2.3.11 + */ + @Parameter + private LayoutType layout; + + /** + * The layout factory that will be used to create the executable archive if no + * explicit layout is set. Alternative layouts implementations can be provided by 3rd + * parties. + * @since 2.3.11 + */ + @Parameter + private LayoutFactory layoutFactory; + + /** + * Return the type of archive that should be used when buiding the image. + * @return the value of the {@code layout} parameter, or {@code null} if the parameter + * is not provided + */ + @Override + protected LayoutType getLayout() { + return this.layout; + } + + /** + * Return the layout factory that will be used to determine the {@link LayoutType} if + * no explicit layout is set. + * @return the value of the {@code layoutFactory} parameter, or {@code null} if the + * parameter is not provided + */ + @Override + protected LayoutFactory getLayoutFactory() { + return this.layoutFactory; + } + @Override public void execute() throws MojoExecutionException { if (this.project.getPackaging().equals("pom")) {