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
This commit is contained in:
parent
efa8d8b877
commit
a7f57091a6
|
|
@ -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.
|
||||
|
|
@ -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).
|
||||
|
||||
|
|
@ -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]
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
|
|
|
|||
|
|
@ -64,6 +64,23 @@ class BuildImageTests extends AbstractArchiveIntegrationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@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")
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@
|
|||
</execution>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
</execution>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
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>
|
||||
|
|
@ -11,19 +11,19 @@
|
|||
<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>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
</execution>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>build-image</goal>
|
||||
<goal>build-image-no-fork</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<image>
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
}
|
||||
|
|
@ -32,11 +32,7 @@ import org.apache.commons.compress.archivers.tar.TarConstants;
|
|||
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;
|
||||
|
|
@ -62,11 +58,7 @@ import org.springframework.util.StringUtils;
|
|||
* @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");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* 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.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.ResolutionScope;
|
||||
|
||||
/**
|
||||
* {@link BuildImageMojo implementation} that does not fork the lifecycle to make sure
|
||||
* that {@code package} ran. This goal should be used when configuring a mojo
|
||||
* {@code execution} in your build. To invoke the goal on the command-line, use
|
||||
* {@link BuildImageForkMojo} instead.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.0.0
|
||||
*/
|
||||
@Mojo(name = "build-image-no-fork", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
|
||||
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
|
||||
requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
|
||||
public class BuildImageNoForkMojo extends BuildImageMojo {
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue