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 896fae15b8..9ae96ccd1f 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; @@ -296,6 +300,33 @@ public class BuildImageTests extends AbstractArchiveIntegrationTests { }); } + @TestTemplate + void whenBuildImageIsInvokedWithZipPackaging(MavenBuild mavenBuild) { + mavenBuild.project("build-image-zip-packaging").goals("package") + .systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT") + .prepare(this::writeLongNameResource).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("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 whenBuildImageIsInvokedWithBuildpacks(MavenBuild mavenBuild) { mavenBuild.project("build-image-custom-buildpacks").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 0000000000..f62888620a --- /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 0000000000..27259ff01a --- /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 18a853e0be..c7c8b5dbc7 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 @@ -50,6 +50,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; @@ -159,6 +160,45 @@ public class BuildImageMojo extends AbstractPackagerMojo { @Parameter private Docker docker; + /** + * 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")) {