Skip to content

Commit

Permalink
Add support for building an image without forking the Maven lifecycle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Nov 22, 2022
1 parent efa8d8b commit a7f5709
Show file tree
Hide file tree
Showing 29 changed files with 139 additions and 48 deletions.
@@ -1,7 +1,8 @@
[[build-image]]
= Packaging OCI Images
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).
Images can be built using the `build-image` goal.
Images can be built on the command-line using the `build-image` goal.
This makes sure that the package lifecycle has run before the image is created.

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

NOTE: Use `build-image-no-fork` when binding the goal to the package lifecycle.
This goal is similar to `build-image` but does not fork the lifecycle to make sure `package` has run.
In the rest of this section, `build-image` is used to refer to either the `build-image` or `build-image-no-fork` goals.

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.
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).

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

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



Expand Down
Expand Up @@ -12,7 +12,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
</execution>
</executions>
Expand Down
Expand Up @@ -64,6 +64,23 @@ void whenBuildImageIsInvokedWithoutRepackageTheArchiveIsRepackagedOnTheFly(Maven
});
}

@TestTemplate
void whenBuildImageIsInvokedOnTheCommandLineWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
mavenBuild.project("build-image-cmd-line").goals("spring-boot:build-image")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.prepare(this::writeLongNameResource).execute((project) -> {
File jar = new File(project, "target/build-image-cmd-line-0.0.1.BUILD-SNAPSHOT.jar");
assertThat(jar).isFile();
File original = new File(project, "target/build-image-cmd-line-0.0.1.BUILD-SNAPSHOT.jar.original");
assertThat(original).doesNotExist();
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image-cmd-line:0.0.1.BUILD-SNAPSHOT")
.contains("---> Test Info buildpack building").contains("---> Test Info buildpack done")
.contains("Successfully built image");
removeImage("build-image-cmd-line", "0.0.1.BUILD-SNAPSHOT");
});
}

@TestTemplate
void whenBuildImageIsInvokedWithClassifierWithoutRepackageTheArchiveIsRepackagedOnTheFly(MavenBuild mavenBuild) {
mavenBuild.project("build-image-classifier").goals("package")
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -41,7 +41,7 @@
</execution>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -35,7 +35,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -25,7 +25,7 @@
</execution>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -3,27 +3,27 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-image</artifactId>
<artifactId>build-image-cmd-line</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
</image>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -20,7 +20,7 @@
<execution>
<goals>
<goal>repackage</goal>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<finalName>final-name</finalName>
Expand Down
Expand Up @@ -27,7 +27,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -20,7 +20,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -25,7 +25,7 @@
</execution>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
</execution>
</executions>
Expand Down
Expand Up @@ -19,7 +19,7 @@
<executions>
<execution>
<goals>
<goal>build-image</goal>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
Expand Down
@@ -0,0 +1,39 @@
/*
* Copyright 2012-2022 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.springframework.boot.maven;

import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;

/**
* {@link BuildImageMojo implementation} that forks the lifecycle to make sure that
* {@code package} ran. This goal is suitable for command-line invocation. If you need to
* configure a mojo {@code execution} in your build, use {@link BuildImageNoForkMojo}
* instead.
*
* @author Stephane Nicoll
* @since 3.0.0
*/
@Mojo(name = "build-image", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Execute(phase = LifecyclePhase.PACKAGE)
public class BuildImageForkMojo extends BuildImageMojo {

}
Expand Up @@ -32,11 +32,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import org.springframework.boot.buildpack.platform.build.AbstractBuildLog;
import org.springframework.boot.buildpack.platform.build.BuildLog;
Expand All @@ -62,11 +58,7 @@
* @author Jeroen Meijer
* @since 2.3.0
*/
@Mojo(name = "build-image", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Execute(phase = LifecyclePhase.PACKAGE)
public class BuildImageMojo extends AbstractPackagerMojo {
public abstract class BuildImageMojo extends AbstractPackagerMojo {

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

0 comments on commit a7f5709

Please sign in to comment.