打包 OCI 映象

該外掛可以透過使用 Cloud Native Buildpacks (CNB) 從 jar 或 war 檔案建立 OCI 映象。可以使用命令列透過 build-image 目標構建映象。這確保在建立映象之前已執行 package 生命週期。

出於安全原因,映象以非 root 使用者身份構建和執行。有關更多詳情,請參閱 CNB 規範

最簡單的入門方法是在專案上呼叫 mvn spring-boot:build-image。可以在每次呼叫 package 階段時自動建立映象,示例如下:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>build-image-no-fork</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>
在將目標繫結到 package 生命週期時,使用 build-image-no-fork。此目標與 build-image 類似,但不會 fork 生命週期以確保 package 已執行。在本節的其餘部分中,build-image 用於指代 build-imagebuild-image-no-fork 目標。
雖然 buildpack 從一個 可執行歸檔檔案 執行,但無需先執行 repackage 目標,因為必要時會自動建立可執行歸檔檔案。當 build-image 重新打包應用程式時,它會應用與 repackage 目標相同的設定,即可以使用 exclude 選項之一排除依賴項。預設情況下會自動排除 spring-boot-devtoolsspring-boot-docker-compose 模組(可以使用 excludeDevtoolsexcludeDockerCompose 屬性控制此行為)。

Docker Daemon

build-image 目標需要訪問 Docker daemon。該目標將檢查本地 Docker CLI 配置檔案 以確定當前的 context 並使用 context 連線資訊與 Docker daemon 通訊。如果無法確定當前的 context 或 context 沒有連線資訊,則該目標將使用預設的本地連線。這適用於所有受支援平臺上的 Docker Engine 無需配置。

可以設定環境變數來配置 build-image 目標以使用替代的本地或遠端連線。下表顯示了環境變數及其值:

環境變數 描述

DOCKER_CONFIG

Docker CLI 配置檔案 的位置,用於確定當前的 context(預設為 $HOME/.docker

DOCKER_CONTEXT

context 的名稱,應使用此名稱從 Docker CLI 配置檔案中檢索主機資訊(覆蓋 DOCKER_HOST

DOCKER_HOST

包含 Docker daemon 的主機和埠的 URL,例如 tcp://192.168.99.100:2376

DOCKER_TLS_VERIFY

當設定為 1 時啟用安全 HTTPS 協議(可選)

DOCKER_CERT_PATH

HTTPS 的證書和金鑰檔案路徑(如果 DOCKER_TLS_VERIFY=1 ,則必需;否則忽略)

Docker daemon 連線資訊也可以使用外掛配置中的 docker 引數提供。下表總結了可用的引數:

引數 描述

context

context 的名稱,應使用此名稱從 Docker CLI 配置檔案 中檢索主機資訊

host

包含 Docker daemon 的主機和埠的 URL,例如 tcp://192.168.99.100:2376

tlsVerify

當設定為 true 時啟用安全 HTTPS 協議(可選)

certPath

HTTPS 的證書和金鑰檔案路徑(如果 tlsVerifytrue ,則必需;否則忽略)

bindHostToBuilder

true 時,host 屬性的值將提供給為 CNB builder 建立的容器(可選)

更多詳情,請參閱 示例

Docker Registry

如果透過 builderrunImage 引數指定的 Docker 映象儲存在需要認證的私有 Docker 映象 registry 中,則可以使用 docker.builderRegistry 引數提供認證憑據。

如果生成的 Docker 映象要釋出到 Docker 映象 registry 中,則可以使用 docker.publishRegistry 引數提供認證憑據。

為使用者認證或身份令牌認證提供了引數。有關支援的認證方法的更多資訊,請查閱用於儲存映象的 Docker registry 文件。

下表總結了 docker.builderRegistrydocker.publishRegistry 可用的引數:

引數 描述

username

Docker 映象 registry 使用者的使用者名稱。使用者認證所需。

password

Docker 映象 registry 使用者的密碼。使用者認證所需。

url

Docker 映象 registry 的地址。使用者認證可選。

email

Docker 映象 registry 使用者的電子郵件地址。使用者認證可選。

token

Docker 映象 registry 使用者的身份令牌。令牌認證所需。

更多詳情,請參閱 示例

映象定製

該外掛會呼叫一個 builder 來協調映象的生成。builder 包含多個 buildpacks ,它們可以檢查應用程式以影響生成的映象。預設情況下,外掛會選擇一個 builder 映象。生成映象的名稱是從專案屬性推斷出來的。

image 引數允許配置 builder 以及它應如何在專案上執行。下表總結了可用的引數及其預設值:

引數 / (使用者屬性) 描述 預設值

builder
(spring-boot.build-image.builder)

要使用的 builder 映象名稱。

paketobuildpacks/builder-jammy-java-tiny:latest

trustBuilder
(spring-boot.build-image.trustBuilder)

是否將 builder 視為 trusted

如果 builder 是以下之一則為 truepaketobuildpacks/builder-jammy-java-tiny, paketobuildpacks/builder-noble-java-tiny, paketobuildpacks/builder-jammy-tiny, paketobuildpacks/builder-jammy-base, paketobuildpacks/builder-jammy-full, paketobuildpacks/builder-jammy-buildpackless-tiny, paketobuildpacks/builder-jammy-buildpackless-base, paketobuildpacks/builder-jammy-buildpackless-full, gcr.io/buildpacks/builder, heroku/builder;否則為 false

imagePlatform
(spring-boot.build-image.imagePlatform)

拉取的任何 builder、run 和 buildpack 映象的平臺(作業系統和架構)。必須採用 OS[/architecture[/variant]] 的形式,例如 linux/amd64, linux/arm64, 或 linux/arm/v5。請參閱正在使用的 builder 文件以確定可用的映象 OS 和架構選項。

沒有預設值,表示應使用主機平臺的配置。

runImage
(spring-boot.build-image.runImage)

要使用的 run 映象名稱。

沒有預設值,表示應使用 Builder 元資料中指定的 run 映象。

name
(spring-boot.build-image.imageName)

映象名稱,用於生成的映象。

docker.io/library/
${project.artifactId}:${project.version}

pullPolicy
(spring-boot.build-image.pullPolicy)

策略,用於確定何時從 registry 拉取 builder 和 run 映象。可接受的值為 ALWAYS, NEVER, 和 IF_NOT_PRESENT

ALWAYS

env

應傳遞給 builder 的環境變數。

buildpacks

builder 在構建映象時應使用的 buildpack。將僅使用指定的 buildpack,覆蓋 builder 中包含的預設 buildpack。Buildpack 引用必須採用以下形式之一:

  • builder 中的 Buildpack - [urn:cnb:builder:]<buildpack ID>[@<version>]

  • 檔案系統目錄中的 Buildpack - [file://]<path>

  • 檔案系統上的 gzipped tar (.tgz) 檔案中的 Buildpack - [file://]<path>/<file name>

  • OCI 映象中的 Buildpack - [docker://]<host>/<repo>[:<tag>][@<digest>]

無,表示 builder 應使用其中包含的 buildpack。

bindings

卷繫結掛載,構建映象時應掛載到 builder 容器的。在建立 builder 容器時,這些繫結將未經解析和驗證直接傳遞給 Docker。繫結必須採用以下形式之一:

  • <host source path>:<container destination path>[:<options>]

  • <host volume name>:<container destination path>[:<options>]

其中 <options> 可以包含:

  • ro 將卷在容器中掛載為只讀

  • rw 將卷在容器中掛載為可讀寫

  • volume-opt=key=value 指定由選項名稱及其值組成的鍵值對

network + (spring-boot.build-image.network)

builder 容器將配置使用的 network driver。提供的值在建立 builder 容器時將未經驗證傳遞給 Docker。

cleanCache + (spring-boot.build-image.cleanCache)

是否在構建前清理快取。

false

verboseLogging

啟用 builder 操作的詳細日誌記錄。

false

publish + (spring-boot.build-image.publish)

是否將生成的映象釋出到 Docker registry。

false

tags

要應用於生成映象的一個或多個附加標籤。提供給 tags 選項的值應該是 完整 映象引用。詳見 tags 部分 獲取更多詳情。

buildWorkspace

builder 和 buildpack 在映象構建期間用於儲存檔案的臨時工作空間。該值可以是命名卷或繫結掛載位置。

Docker daemon 中的命名卷,名稱源自映象名稱。

buildCache

包含 buildpack 建立的層並在映象構建過程中使用的快取。該值可以是命名卷或繫結掛載位置。

Docker daemon 中的命名卷,名稱源自映象名稱。

launchCache

包含 buildpack 建立的層並在映象啟動過程中使用的快取。該值可以是命名卷或繫結掛載位置。

Docker daemon 中的命名卷,名稱源自映象名稱。

createdDate
(spring-boot.build-image.createdDate)

一個日期,將用於設定生成的映象元資料中的 Created 欄位。該值必須是 ISO 8601 Instant 格式的字串,或者 now 表示使用當前日期和時間。

啟用 構建可重現性 的固定日期。

applicationDirectory
(spring-boot.build-image.applicationDirectory)

應用程式內容將上傳到 builder 映象中該目錄的路徑。在生成的映象中,應用程式內容也將位於此位置。

/workspace

securityOptions

安全選項,將應用於 builder 容器的,作為字串值陣列提供:

在 Linux 和 macOS 上為 ["label=disable"],在 Windows 上為 []

外掛使用編譯器的外掛配置或 maven.compiler.target 屬性檢測專案的目標 Java 相容性。使用預設的 Paketo builder 和 buildpack 時,外掛會指示 buildpack 安裝相同的 Java 版本。你可以覆蓋此行為,如 builder 配置 示例所示。

更多詳情,請參閱 示例

標籤格式

提供給 tags 選項的值應該是 完整 映象引用。可接受的格式為 [domainHost:port/][path/]name[:tag][@digest]

如果缺少 domain,則預設為 docker.io。如果缺少 path,則預設為 library。如果缺少 tag,則預設為 latest

一些示例:

  • my-image 導致映象引用 docker.io/library/my-image:latest

  • my-repository/my-image 導致 docker.io/my-repository/my-image:latest

  • example.com/my-repository/my-image:1.0.0 將按原樣使用

spring-boot:build-image

org.springframework.boot:spring-boot-maven-plugin:3.4.5

使用 buildpack 將應用程式打包到 OCI 映象中,會 fork 生命週期以確保 package 已執行。此目標適合在命令列呼叫。如果你需要在構建中配置一個 goal execution,請改用 build-image-no-fork

必需引數

名稱 型別 預設值

sourceDirectory

檔案

${project.build.directory}

可選引數

名稱 型別 預設值

classifier

String

docker

Docker

excludeDevtools

boolean

true

excludeDockerCompose

boolean

true

excludeGroupIds

String

excludes

List

image

Image

includeSystemScope

boolean

false

includeTools

boolean

true

includes

List

layers

Layers

layout

LayoutType

layoutFactory

LayoutFactory

loaderImplementation

LoaderImplementation

mainClass

String

skip

boolean

false

引數詳情

classifier

查詢源歸檔檔案時使用的 Classifier。

名稱

classifier

型別

java.lang.String

預設值

使用者屬性

始於

2.3.0

docker

Docker 配置選項。

名稱

docker

型別

org.springframework.boot.maven.Docker

預設值

使用者屬性

始於

2.4.0

excludeDevtools

從重新打包的歸檔檔案中排除 Spring Boot devtools。

名稱

excludeDevtools

型別

boolean

預設值

true

使用者屬性

spring-boot.repackage.excludeDevtools

始於

1.3.0

excludeDockerCompose

從重新打包的歸檔檔案中排除 Spring Boot dev services。

名稱

excludeDockerCompose

型別

boolean

預設值

true

使用者屬性

spring-boot.repackage.excludeDockerCompose

始於

3.1.0

excludeGroupIds

逗號分隔的要排除的 groupId 名稱列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應是逗號分隔的,其中元件使用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

image

映象配置,包含 builder, runImage, name, env, cleanCache, verboseLogging, pullPolicy, 和 publish 等選項。

名稱

image

型別

org.springframework.boot.maven.Image

預設值

使用者屬性

始於

2.3.0

includeSystemScope

包含 system 範圍的依賴。

名稱

includeSystemScope

型別

boolean

預設值

false

使用者屬性

始於

1.4.0

includeTools

包含 JAR 工具。

名稱

includeTools

型別

boolean

預設值

true

使用者屬性

始於

3.3.0

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應是逗號分隔的,其中元件使用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

layers

層配置,包含停用層建立、排除層工具 jar 以及提供自定義層配置檔案的選項。

名稱

layers

型別

org.springframework.boot.maven.Layers

預設值

使用者屬性

始於

2.3.0

layout

歸檔檔案的型別(對應於依賴項在其中佈局的方式)。可能的值為 JAR, WAR, ZIP, DIR, NONE。預設根據歸檔檔案型別猜測。

名稱

layout

型別

org.springframework.boot.maven.AbstractPackagerMojo$LayoutType

預設值

使用者屬性

始於

2.3.11

layoutFactory

如果沒有設定顯式佈局,則將用於建立可執行歸檔檔案的佈局工廠。第三方可以提供其他佈局實現。

名稱

layoutFactory

型別

org.springframework.boot.loader.tools.LayoutFactory

預設值

使用者屬性

始於

2.3.11

loaderImplementation

應使用的 loader 實現。

名稱

loaderImplementation

型別

org.springframework.boot.loader.tools.LoaderImplementation

預設值

使用者屬性

始於

3.2.0

mainClass

主類的名稱。如果未指定,將使用找到的第一個包含 main 方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

始於

1.0.0

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.build-image.skip

始於

2.3.0

sourceDirectory

包含源歸檔檔案的目錄。

名稱

sourceDirectory

型別

java.io.File

預設值

${project.build.directory}

使用者屬性

始於

2.3.0

spring-boot:build-image-no-fork

org.springframework.boot:spring-boot-maven-plugin:3.4.5

使用 buildpack 將應用程式打包成 OCI 映象,但無需 fork 生命週期。此目標應在構建中配置目標 execution 時使用。要在命令列上呼叫此目標,請改用 build-image

必需引數

名稱 型別 預設值

sourceDirectory

檔案

${project.build.directory}

可選引數

名稱 型別 預設值

classifier

String

docker

Docker

excludeDevtools

boolean

true

excludeDockerCompose

boolean

true

excludeGroupIds

String

excludes

List

image

Image

includeSystemScope

boolean

false

includeTools

boolean

true

includes

List

layers

Layers

layout

LayoutType

layoutFactory

LayoutFactory

loaderImplementation

LoaderImplementation

mainClass

String

skip

boolean

false

引數詳情

classifier

查詢源歸檔檔案時使用的 Classifier。

名稱

classifier

型別

java.lang.String

預設值

使用者屬性

始於

2.3.0

docker

Docker 配置選項。

名稱

docker

型別

org.springframework.boot.maven.Docker

預設值

使用者屬性

始於

2.4.0

excludeDevtools

從重新打包的歸檔檔案中排除 Spring Boot devtools。

名稱

excludeDevtools

型別

boolean

預設值

true

使用者屬性

spring-boot.repackage.excludeDevtools

始於

1.3.0

excludeDockerCompose

從重新打包的歸檔檔案中排除 Spring Boot dev services。

名稱

excludeDockerCompose

型別

boolean

預設值

true

使用者屬性

spring-boot.repackage.excludeDockerCompose

始於

3.1.0

excludeGroupIds

逗號分隔的要排除的 groupId 名稱列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應是逗號分隔的,其中元件使用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

image

映象配置,包含 builder, runImage, name, env, cleanCache, verboseLogging, pullPolicy, 和 publish 等選項。

名稱

image

型別

org.springframework.boot.maven.Image

預設值

使用者屬性

始於

2.3.0

includeSystemScope

包含 system 範圍的依賴。

名稱

includeSystemScope

型別

boolean

預設值

false

使用者屬性

始於

1.4.0

includeTools

包含 JAR 工具。

名稱

includeTools

型別

boolean

預設值

true

使用者屬性

始於

3.3.0

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及可選的 classifier 元件。當配置為屬性時,值應是逗號分隔的,其中元件使用冒號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

layers

層配置,包含停用層建立、排除層工具 jar 以及提供自定義層配置檔案的選項。

名稱

layers

型別

org.springframework.boot.maven.Layers

預設值

使用者屬性

始於

2.3.0

layout

歸檔檔案的型別(對應於依賴項在其中佈局的方式)。可能的值為 JAR, WAR, ZIP, DIR, NONE。預設根據歸檔檔案型別猜測。

名稱

layout

型別

org.springframework.boot.maven.AbstractPackagerMojo$LayoutType

預設值

使用者屬性

始於

2.3.11

layoutFactory

如果沒有設定顯式佈局,則將用於建立可執行歸檔檔案的佈局工廠。第三方可以提供其他佈局實現。

名稱

layoutFactory

型別

org.springframework.boot.loader.tools.LayoutFactory

預設值

使用者屬性

始於

2.3.11

loaderImplementation

應使用的 loader 實現。

名稱

loaderImplementation

型別

org.springframework.boot.loader.tools.LoaderImplementation

預設值

使用者屬性

始於

3.2.0

mainClass

主類的名稱。如果未指定,將使用找到的第一個包含 main 方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

始於

1.0.0

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.build-image.skip

始於

2.3.0

sourceDirectory

包含源歸檔檔案的目錄。

名稱

sourceDirectory

型別

java.io.File

預設值

${project.build.directory}

使用者屬性

始於

2.3.0

示例

自定義映象構建器

如果需要自定義用於建立映象的構建器或用於啟動已構建映象的執行映象,請按照以下示例配置外掛

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<builder>mine/java-cnb-builder</builder>
						<runImage>mine/java-cnb-run</runImage>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

此配置將使用名稱為 mine/java-cnb-builder 和標籤為 latest 的構建器映象,以及名稱為 mine/java-cnb-run 和標籤為 latest 的執行映象。

構建器和執行映象也可以在命令列上指定,如下例所示

$ mvn spring-boot:build-image -Dspring-boot.build-image.builder=mine/java-cnb-builder -Dspring-boot.build-image.runImage=mine/java-cnb-run

構建器配置

如果構建器透過環境變數暴露配置選項,可以使用 env 屬性進行設定。

以下是配置 JVM 版本的示例,該版本由 Paketo Java buildpacks 在構建時使用

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<BP_JVM_VERSION>17</BP_JVM_VERSION>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果在構建器執行的 Docker daemon 與 buildpacks 下載工件的網路位置之間存在網路代理,您需要配置構建器使用該代理。使用 Paketo 構建器時,可以透過設定 HTTPS_PROXY 和/或 HTTP_PROXY 環境變數來實現,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<HTTP_PROXY>http://proxy.example.com</HTTP_PROXY>
							<HTTPS_PROXY>https://proxy.example.com</HTTPS_PROXY>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

執行時 JVM 配置

Paketo Java buildpacks 透過設定 JAVA_TOOL_OPTIONS 環境變數來配置 JVM 執行時環境。可以修改 buildpack 提供的 JAVA_TOOL_OPTIONS 值,以便在容器中啟動應用程式映象時自定義 JVM 執行時行為。

應儲存在映象中並應用於每次部署的環境變數修改,可以按照Paketo 文件中的描述進行設定,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<env>
							<BPE_DELIM_JAVA_TOOL_OPTIONS xml:space="preserve"> </BPE_DELIM_JAVA_TOOL_OPTIONS>
							<BPE_APPEND_JAVA_TOOL_OPTIONS>-XX:+HeapDumpOnOutOfMemoryError</BPE_APPEND_JAVA_TOOL_OPTIONS>
						</env>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

自定義映象名稱

預設情況下,映象名稱從專案的 artifactIdversion 推斷,例如 docker.io/library/${project.artifactId}:${project.version}。您可以控制該名稱,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<name>example.com/library/${project.artifactId}</name>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
此配置未提供顯式標籤,因此使用 latest。也可以指定標籤,可以使用 ${project.version}、構建中可用的任何屬性或硬編碼的版本。

映象名稱也可以在命令列上指定,如下例所示

$ mvn spring-boot:build-image -Dspring-boot.build-image.imageName=example.com/library/my-app:v1

構建包

預設情況下,構建器將使用構建器映象中包含的構建包,並按照預定義的順序應用它們。可以提供一組替代的構建包,以應用不包含在構建器中的構建包,或更改包含的構建包的順序。當提供一個或多個構建包時,僅應用指定的構建包。

以下示例指示構建器使用打包在 .tgz 檔案中的自定義構建包,然後使用構建器中包含的構建包。

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildpacks>
							<buildpack>file:///path/to/example-buildpack.tgz</buildpack>
							<buildpack>urn:cnb:builder:paketo-buildpacks/java</buildpack>
						</buildpacks>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

可以透過以下任何一種形式指定構建包。

CNB Builder 中的構建包(如果構建器中只有一個與 buildpack-id 匹配的構建包,則可以省略版本)

包含構建包內容的目錄路徑(Windows 上不支援)

  • file:///path/to/buildpack/

  • /path/to/buildpack/

包含構建包內容的 gzipped tar 檔案路徑

  • file:///path/to/buildpack.tgz

  • /path/to/buildpack.tgz

包含打包的構建包的 OCI 映象

  • docker://example/buildpack

  • docker:///example/buildpack:latest

  • docker:///example/buildpack@sha256:45b23dee08…​

  • example/buildpack

  • example/buildpack:latest

  • example/buildpack@sha256:45b23dee08…​

映象釋出

可以透過啟用 publish 選項將生成的映象釋出到 Docker 登錄檔。

如果 Docker 登錄檔需要身份驗證,可以使用 docker.publishRegistry 引數配置憑據。如果 Docker 登錄檔不需要身份驗證,則可以省略 docker.publishRegistry 配置。

映象將釋出到的登錄檔由映象名稱的登錄檔部分確定(在本例中為 docker.example.com)。如果配置了 docker.publishRegistry 憑據幷包含 url 引數,此值將傳遞給登錄檔,但不用於確定釋出登錄檔的位置。
<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<name>docker.example.com/library/${project.artifactId}</name>
						<publish>true</publish>
					</image>
					<docker>
						<publishRegistry>
							<username>user</username>
							<password>secret</password>
						</publishRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

publish 選項也可以在命令列上指定,如下例所示

$ mvn spring-boot:build-image -Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1 -Dspring-boot.build-image.publish=true

在命令列上使用帶有身份驗證的 publish 選項時,您可以使用屬性提供憑據,如下例所示

$ mvn spring-boot:build-image \
      -Ddocker.publishRegistry.username=user \
      -Ddocker.publishRegistry.password=secret \
      -Ddocker.publishRegistry.url=docker.example.com \
      -Dspring-boot.build-image.publish=true \
      -Dspring-boot.build-image.imageName=docker.example.com/library/my-app:v1

並在 XML 配置中引用這些屬性

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<publishRegistry>
							<url>${docker.publishRegistry.url}</url>
							<username>${docker.publishRegistry.username}</username>
							<password>${docker.publishRegistry.password}</password>
						</publishRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

構建器快取和工作空間配置

CNB 構建器快取構建和啟動映象時使用的層。預設情況下,這些快取作為命名卷儲存在 Docker daemon 中,其名稱派生自目標映象的完整名稱。如果映象名稱經常更改,例如在映象名稱中使用專案版本作為標籤時,則快取可能會頻繁失效。

可以配置快取卷使用替代名稱,以更好地控制快取生命週期,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildCache>
							<volume>
								<name>cache-${project.artifactId}.build</name>
							</volume>
						</buildCache>
						<launchCache>
							<volume>
								<name>cache-${project.artifactId}.launch</name>
							</volume>
						</launchCache>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

構建器和構建包在映象構建期間需要一個儲存臨時檔案的位置。預設情況下,此臨時構建工作空間儲存在命名卷中。

可以將快取和構建工作空間配置為使用繫結掛載而非命名卷,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<image>
						<buildWorkspace>
							<bind>
								<source>/tmp/cache-${project.artifactId}.work</source>
							</bind>
						</buildWorkspace>
						<buildCache>
							<bind>
								<source>/tmp/cache-${project.artifactId}.build</source>
							</bind>
						</buildCache>
						<launchCache>
							<bind>
								<source>/tmp/cache-${project.artifactId}.launch</source>
							</bind>
						</launchCache>
					</image>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Docker 配置

minikube 的 Docker 配置

外掛可以與minikube 提供的 Docker daemon 通訊,而不是預設的本地連線。

在 Linux 和 macOS 上,minikube 啟動後,可以使用命令 eval $(minikube docker-env) 設定環境變數。

也可以透過提供與以下示例中類似的連線詳情,將外掛配置為使用 minikube daemon

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>tcp://192.168.99.100:2376</host>
						<tlsVerify>true</tlsVerify>
						<certPath>/home/user/.minikube/certs</certPath>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

podman 的 Docker 配置

外掛可以與podman 容器引擎通訊。

可以透過提供與以下示例中類似的連線詳情,將外掛配置為使用 podman 本地連線

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>unix:///run/user/1000/podman/podman.sock</host>
						<bindHostToBuilder>true</bindHostToBuilder>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
安裝 colima CLI 後,可以使用命令 podman info --format='{{.Host.RemoteSocket.Path}}' 獲取此示例中所示的 docker.host 配置屬性的值。

Colima 的 Docker 配置

可以使用以下命令設定 DOCKER_HOST 環境變數

$ export DOCKER_HOST=$(docker context inspect colima -f '{{.Endpoints.docker.Host}}')

也可以透過提供與以下示例中類似的連線詳情,將外掛配置為使用 Colima daemon

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<host>unix:///${user.home}/.colima/docker.sock</host>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Docker 身份驗證配置

如果構建器或執行映象儲存在支援使用者身份驗證的私有 Docker 登錄檔中,可以使用 docker.builderRegistry 引數提供身份驗證詳情,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<builderRegistry>
							<username>user</username>
							<password>secret</password>
							<url>https://docker.example.com/v1/</url>
							<email>[email protected]</email>
						</builderRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果構建器或執行映象儲存在支援令牌身份驗證的私有 Docker 登錄檔中,可以使用 docker.builderRegistry 引數提供令牌值,如下例所示

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<docker>
						<builderRegistry>
							<token>9cbaf023786cd7...</token>
						</builderRegistry>
					</docker>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>