Skip to content

Commit a7f5709

Browse files
committed
Add support for building an image without forking the Maven lifecycle
This commit adds a new "build-image-no-fork" goal that behaves exactly as "build-image", except it does not fork the lifecycle. This is a common pattern in maven plugins where a goal can be invoked on the command-line and conveniently make sure that a certain phase has run prior to its execution. The new goal is suitable for binding the goal in a phase, typically package, and rely on it rather than invoking the goal. This makes sure that the lifecycle is forked, which would run everything again. Closes gh-26455
1 parent efa8d8b commit a7f5709

File tree

29 files changed

+139
-48
lines changed

29 files changed

+139
-48
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/asciidoc/packaging-oci-image.adoc

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[[build-image]]
22
= Packaging OCI Images
33
The plugin can create an https://github.com/opencontainers/image-spec[OCI image] from a jar or war file using https://buildpacks.io/[Cloud Native Buildpacks] (CNB).
4-
Images can be built using the `build-image` goal.
4+
Images can be built on the command-line using the `build-image` goal.
5+
This makes sure that the package lifecycle has run before the image is created.
56

67
NOTE: For security reasons, images build and run as non-root users.
78
See the {buildpacks-reference}/reference/spec/platform-api/#users[CNB specification] for more details.
@@ -14,6 +15,10 @@ It is possible to automate the creation of an image whenever the `package` phase
1415
include::../maven/packaging-oci-image/pom.xml[tags=packaging-oci-image]
1516
----
1617

18+
NOTE: Use `build-image-no-fork` when binding the goal to the package lifecycle.
19+
This goal is similar to `build-image` but does not fork the lifecycle to make sure `package` has run.
20+
In the rest of this section, `build-image` is used to refer to either the `build-image` or `build-image-no-fork` goals.
21+
1722
TIP: While the buildpack runs from an <<packaging,executable archive>>, it is not necessary to execute the `repackage` goal first as the executable archive is created automatically if necessary.
1823
When the `build-image` repackages the application, it applies the same settings as the `repackage` goal would, that is dependencies can be excluded using one of the exclude options, and Devtools is automatically excluded by default (you can control that using the `excludeDevtools` property).
1924

@@ -209,6 +214,7 @@ You can override this behaviour as shown in the <<build-image.examples.builder-c
209214
For more details, see also <<build-image.examples,examples>>.
210215

211216
include::goals/build-image.adoc[leveloffset=+1]
217+
include::goals/build-image-no-fork.adoc[leveloffset=+1]
212218

213219

214220

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/maven/packaging-oci-image/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<executions>
1313
<execution>
1414
<goals>
15-
<goal>build-image</goal>
15+
<goal>build-image-no-fork</goal>
1616
</goals>
1717
</execution>
1818
</executions>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/BuildImageTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ void whenBuildImageIsInvokedWithoutRepackageTheArchiveIsRepackagedOnTheFly(Maven
6464
});
6565
}
6666

67+
@TestTemplate
68+
void whenBuildImageIsInvokedOnTheCommandLineWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
69+
mavenBuild.project("build-image-cmd-line").goals("spring-boot:build-image")
70+
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
71+
.prepare(this::writeLongNameResource).execute((project) -> {
72+
File jar = new File(project, "target/build-image-cmd-line-0.0.1.BUILD-SNAPSHOT.jar");
73+
assertThat(jar).isFile();
74+
File original = new File(project, "target/build-image-cmd-line-0.0.1.BUILD-SNAPSHOT.jar.original");
75+
assertThat(original).doesNotExist();
76+
assertThat(buildLog(project)).contains("Building image")
77+
.contains("docker.io/library/build-image-cmd-line:0.0.1.BUILD-SNAPSHOT")
78+
.contains("---> Test Info buildpack building").contains("---> Test Info buildpack done")
79+
.contains("Successfully built image");
80+
removeImage("build-image-cmd-line", "0.0.1.BUILD-SNAPSHOT");
81+
});
82+
}
83+
6784
@TestTemplate
6885
void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
6986
mavenBuild.project("build-image-classifier").goals("package")

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-bad-buildpack/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-bindings/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-builder-error/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-caches-multiple/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-caches/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-classifier-source-with-repackage/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</execution>
4242
<execution>
4343
<goals>
44-
<goal>build-image</goal>
44+
<goal>build-image-no-fork</goal>
4545
</goals>
4646
<configuration>
4747
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-classifier-source/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<executions>
3636
<execution>
3737
<goals>
38-
<goal>build-image</goal>
38+
<goal>build-image-no-fork</goal>
3939
</goals>
4040
<configuration>
4141
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-classifier-with-repackage/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</execution>
2626
<execution>
2727
<goals>
28-
<goal>build-image</goal>
28+
<goal>build-image-no-fork</goal>
2929
</goals>
3030
<configuration>
3131
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-classifier/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-cmd-line/pom.xml

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.springframework.boot.maven.it</groupId>
6-
<artifactId>build-image</artifactId>
6+
<artifactId>build-image-cmd-line</artifactId>
77
<version>0.0.1.BUILD-SNAPSHOT</version>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1010
<maven.compiler.source>@java.version@</maven.compiler.source>
1111
<maven.compiler.target>@java.version@</maven.compiler.target>
1212
</properties>
1313
<build>
14-
<plugins>
15-
<plugin>
16-
<groupId>@project.groupId@</groupId>
17-
<artifactId>@project.artifactId@</artifactId>
18-
<version>@project.version@</version>
19-
<executions>
20-
<execution>
21-
<goals>
22-
<goal>build-image</goal>
23-
</goals>
24-
</execution>
25-
</executions>
26-
</plugin>
27-
</plugins>
14+
<pluginManagement>
15+
<plugins>
16+
<plugin>
17+
<groupId>@project.groupId@</groupId>
18+
<artifactId>@project.artifactId@</artifactId>
19+
<version>@project.version@</version>
20+
<configuration>
21+
<image>
22+
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
23+
</image>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</pluginManagement>
2828
</build>
2929
</project>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-custom-builder/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-custom-buildpacks/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-custom-name/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-empty-env-entry/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-final-name/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<execution>
2121
<goals>
2222
<goal>repackage</goal>
23-
<goal>build-image</goal>
23+
<goal>build-image-no-fork</goal>
2424
</goals>
2525
<configuration>
2626
<finalName>final-name</finalName>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-multi-module/app/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<executions>
2828
<execution>
2929
<goals>
30-
<goal>build-image</goal>
30+
<goal>build-image-no-fork</goal>
3131
</goals>
3232
<configuration>
3333
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-network/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-publish/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-tags/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-war-packaging/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<executions>
2121
<execution>
2222
<goals>
23-
<goal>build-image</goal>
23+
<goal>build-image-no-fork</goal>
2424
</goals>
2525
<configuration>
2626
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-with-repackage/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</execution>
2626
<execution>
2727
<goals>
28-
<goal>build-image</goal>
28+
<goal>build-image-no-fork</goal>
2929
</goals>
3030
<configuration>
3131
<image>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image-zip-packaging/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
</execution>
2525
</executions>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/projects/build-image/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<executions>
2020
<execution>
2121
<goals>
22-
<goal>build-image</goal>
22+
<goal>build-image-no-fork</goal>
2323
</goals>
2424
<configuration>
2525
<image>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2022 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.springframework.boot.maven;
18+
19+
import org.apache.maven.plugins.annotations.Execute;
20+
import org.apache.maven.plugins.annotations.LifecyclePhase;
21+
import org.apache.maven.plugins.annotations.Mojo;
22+
import org.apache.maven.plugins.annotations.ResolutionScope;
23+
24+
/**
25+
* {@link BuildImageMojo implementation} that forks the lifecycle to make sure that
26+
* {@code package} ran. This goal is suitable for command-line invocation. If you need to
27+
* configure a mojo {@code execution} in your build, use {@link BuildImageNoForkMojo}
28+
* instead.
29+
*
30+
* @author Stephane Nicoll
31+
* @since 3.0.0
32+
*/
33+
@Mojo(name = "build-image", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
34+
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
35+
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
36+
@Execute(phase = LifecyclePhase.PACKAGE)
37+
public class BuildImageForkMojo extends BuildImageMojo {
38+
39+
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
import org.apache.maven.artifact.Artifact;
3333
import org.apache.maven.plugin.MojoExecutionException;
3434
import org.apache.maven.plugin.logging.Log;
35-
import org.apache.maven.plugins.annotations.Execute;
36-
import org.apache.maven.plugins.annotations.LifecyclePhase;
37-
import org.apache.maven.plugins.annotations.Mojo;
3835
import org.apache.maven.plugins.annotations.Parameter;
39-
import org.apache.maven.plugins.annotations.ResolutionScope;
4036

4137
import org.springframework.boot.buildpack.platform.build.AbstractBuildLog;
4238
import org.springframework.boot.buildpack.platform.build.BuildLog;
@@ -62,11 +58,7 @@
6258
* @author Jeroen Meijer
6359
* @since 2.3.0
6460
*/
65-
@Mojo(name = "build-image", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
66-
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
67-
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
68-
@Execute(phase = LifecyclePhase.PACKAGE)
69-
public class BuildImageMojo extends AbstractPackagerMojo {
61+
public abstract class BuildImageMojo extends AbstractPackagerMojo {
7062

7163
static {
7264
System.setProperty("org.slf4j.simpleLogger.log.org.apache.http.wire", "ERROR");

0 commit comments

Comments
 (0)