2020-01-23 23:35:45 +08:00
[[build-image]]
== Packaging OCI Images
2020-01-31 07:54:15 +08:00
The plugin can create an https://github.com/opencontainers/image-spec[OCI image] using https://buildpacks.io/[Cloud Native Buildpacks].
2020-01-23 23:35:45 +08:00
Images can be built using the `build-image` goal and a local Docker installation.
2020-02-19 10:16:46 +08:00
The easiest way to get started is to invoke `mvn spring-boot:build-image` on a project.
2020-01-23 23:35:45 +08:00
It is possible to automate the creation of an image whenever the `package` phase is invoked, as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
2020-01-31 07:54:15 +08:00
<version>{gradle-project-version}</version>
2020-01-23 23:35:45 +08:00
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
----
TIP: While the buildpack runs from an <<repackage,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, i.e. 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).
2020-01-31 09:02:47 +08:00
2020-01-28 03:56:42 +08:00
[[build-image-customization]]
=== Image Customizations
2020-01-31 07:54:15 +08:00
The plugin invokes a {buildpacks-reference}/concepts/components/builder/[builder] to orchestrate the generation of an image.
The builder includes multiple {buildpacks-reference}/concepts/components/buildpack[buildpacks] that can inspect the application to influence the generated image.
By default, the plugin chooses a builder image.
The name of the generated image is deduced from project properties.
2020-01-28 03:56:42 +08:00
The `image` parameter allows to configure how the builder should operate on the project.
2020-03-18 07:40:37 +08:00
The following table summarizes the available parameters and their default values:
2020-01-28 03:56:42 +08:00
|===
2020-03-18 07:40:37 +08:00
| Parameter | Description | User property | Default value
2020-01-28 03:56:42 +08:00
| `builder`
2020-01-31 07:54:15 +08:00
| Name of the Builder image to use.
2020-03-18 07:40:37 +08:00
| `spring-boot.build-image.builder`
2020-02-07 07:23:03 +08:00
| `cloudfoundry/cnb:0.0.53-bionic`
2020-01-28 03:56:42 +08:00
| `name`
2020-01-31 07:54:15 +08:00
| {spring-boot-api}/buildpack/platform/docker/type/ImageReference.html#of-java.lang.String-[Image name] for the generated image.
2020-03-18 07:40:37 +08:00
| `spring-boot.build-image.imageName`
2020-01-28 03:56:42 +08:00
| `docker.io/library/${project.artifactId}:${project.version}`
| `env`
2020-01-31 07:54:15 +08:00
| Environment variables that should be passed to the builder.
2020-01-28 03:56:42 +08:00
|
2020-03-18 07:40:37 +08:00
|
2020-01-28 03:56:42 +08:00
| `cleanCache`
| Whether to clean the cache before building.
2020-03-18 07:40:37 +08:00
|
2020-01-28 03:56:42 +08:00
| `false`
| `verboseLogging`
2020-01-31 07:54:15 +08:00
| Enables verbose logging of builder operations.
2020-03-18 07:40:37 +08:00
|
2020-01-28 03:56:42 +08:00
| `false`
|===
For more details, see <<build-image-example-custom-image-builder,custom image builder>> and <<build-image-example-custom-image-name,custom image name>>.
2020-01-23 23:35:45 +08:00
include::goals/build-image.adoc[leveloffset=+1]
2020-01-31 09:02:47 +08:00
2020-01-23 23:35:45 +08:00
[[build-image-examples]]
=== Examples
2020-01-31 09:02:47 +08:00
2020-01-23 23:35:45 +08:00
[[build-image-example-custom-image-builder]]
==== Custom Image Builder
2020-03-18 07:40:37 +08:00
If you need to customize the builder used to create the image, configure the plugin as shown in the following example:
2020-01-23 23:35:45 +08:00
[source,xml,indent=0,subs="verbatim,attributes"]
----
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
2020-01-31 07:54:15 +08:00
<version>{gradle-project-version}</version>
2020-01-28 03:12:00 +08:00
<configuration>
<image>
<builder>mine/java-cnb-builder</builder>
</image>
</configuration>
2020-01-23 23:35:45 +08:00
</plugin>
</plugins>
</build>
</project>
----
2020-01-31 07:54:15 +08:00
This configuration will use a builder image with the name `mine/java-cnb-builder` and the tag `latest`.
2020-01-23 23:35:45 +08:00
2020-03-18 07:40:37 +08:00
The builder can be specified on the command line as well, as shown in this example:
[indent=0]
----
$ mvn spring-boot:build-image -Dspring-boot.build-image:builder=mine/java-cnb-builder
----
2020-01-28 03:56:42 +08:00
If the builder exposes configuration options, those can be set using the `env` attributes, as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
2020-01-31 07:54:15 +08:00
<version>{gradle-project-version}</version>
2020-01-28 03:56:42 +08:00
<configuration>
<image>
<builder>mine/java-cnb-builder</builder>
<env>
<BP_JAVA_VERSION>13.0.1</BP_JAVA_VERSION>
</env>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
----
The example above assumes that `mine/java-cnb-builder` defines a `BP_JAVA_VERSION` property (typically used to customize the JDK version the image should use).
2020-01-23 23:35:45 +08:00
[[build-image-example-custom-image-name]]
==== Custom Image Name
2020-01-31 07:54:15 +08:00
By default, the image name is inferred from the `artifactId` and the `version` of the project, something like `docker.io/library/${project.artifactId}:${project.version}`.
2020-01-23 23:35:45 +08:00
You can take control over the name, as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
----
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
2020-01-31 07:54:15 +08:00
<version>{gradle-project-version}</version>
2020-01-28 03:12:00 +08:00
<configuration>
<image>
<name>example.com/library/${project.artifactId}</name>
</image>
</configuration>
2020-01-23 23:35:45 +08:00
</plugin>
</plugins>
</build>
</project>
----
2020-01-31 07:54:15 +08:00
Note that this configuration does not provide an explicit tag so `latest` is used.
2020-03-18 07:40:37 +08:00
It is possible to specify a tag as well, either using `${project.version}`, any property available in the build or a hardcoded version.
The image name can be specified on the command line as well, as shown in this example:
[indent=0]
----
$ mvn spring-boot:build-image -Dspring-boot.build-image:imageName=example.com/library/v1
----