事前処理

Spring AOT は、ビルド時にアプリケーションを分析し、最適化されたバージョンを生成するプロセスです。ネイティブイメージで Spring ApplicationContext を実行することは必須の手順です。

Spring Boot での GraalVM ネイティブイメージサポートの概要については、リファレンスドキュメントを確認してください。

Spring Boot Maven プラグインは、アプリケーションとテストコードの両方で AOT 処理を実行するために使用できるゴールを提供します。

アプリケーションの処理

この機能を使用するようにアプリケーションを構成するには、次の例に示すように、process-aot ゴールの実行を追加します。

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-aot</id>
			<goals>
				<goal>process-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>

BeanFactory はビルド時に完全に準備されるため、条件も評価されます。これは、実行時に通常の Spring Boot アプリケーションが行うことと比べて重要な違いがあります。たとえば、特定の機能をオプトインまたはオプトアウトする場合は、ビルド時に使用する環境を構成する必要があります。そのため、process-aot ゴールは多くのプロパティを run ゴールと共有します。

ネイティブプロファイルの使用

プロジェクトの parent として spring-boot-starter-parent を使用する場合、native プロファイルを使用して、ネイティブイメージのビルドに必要な手順を合理化できます。

native プロファイルは、以下を構成します。

  • Spring Boot Maven プラグインがプロジェクトに適用されている場合の process-aot の実行。

  • build-image がネイティブイメージを生成するための適切な設定。

  • 特にネイティブビルドツール Maven プラグイン (英語) の適切なデフォルト:

    • 再パッケージ化された jar 形式を認識しないため、プラグインがメインの jar ファイルではなく生のクラスパスを使用していることを確認します。

    • 適切な GraalVM バージョンが利用可能であることを検証します。

    • サードパーティの到達可能性メタデータをダウンロードします。

native プロファイルを活用するには、次の例に示すように、アプリケーションを表すモジュールで 2 つのプラグインを定義する必要があります。

<plugin>
	<groupId>org.graalvm.buildtools</groupId>
	<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

単一のプロジェクトは、コマンドラインで Cloud Native Buildpacks またはネイティブイメージビルドツールを使用してネイティブイメージの生成をトリガーできます。

マルチモジュールプロジェクトで native プロファイルを使用するには、native プロファイルのカスタマイズを作成して、推奨する手法を呼び出すことができます。

package フェーズで Cloud Native Buildpacks をバインドするには、マルチモジュールプロジェクトのルート POM に次を追加します。

<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>build-image-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>

以下の例は、ネイティブビルドツールに対して同じことを行います。

<profile>
	<id>native</id>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.graalvm.buildtools</groupId>
					<artifactId>native-maven-plugin</artifactId>
					<executions>
						<execution>
							<id>build-image</id>
							<goals>
								<goal>compile-no-fork</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</profile>

上記が整ったら、次の例に示すように、マルチモジュールプロジェクトをビルドし、関連するサブモジュールでネイティブイメージを生成できます。

$ mvn package -Pnative
「関連する」サブモジュールは、Spring Boot アプリケーションを表すモジュールです。このようなモジュールは、上記のようにネイティブビルドツールと Spring Boot プラグインを定義する必要があります。

spring-boot:process-aot

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

アプリケーションで AOT エンジンを呼び出します。

必須パラメーター

名前 タイプ デフォルト

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

${project.build.directory}/spring-aot/main/classes

generatedResources

File

${project.build.directory}/spring-aot/main/resources

generatedSources

File

${project.build.directory}/spring-aot/main/sources

オプションのパラメーター

名前 タイプ デフォルト

arguments

String[]

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

mainClass

String

profiles

String[]

skip

boolean

false

systemPropertyVariables

Map

パラメーターの詳細

arguments

AOT 処理で考慮する必要があるアプリケーション引数。

名前

arguments

タイプ

java.lang.String[]

デフォルト値

ユーザープロパティ

開始

classesDirectory

アーカイブにパッケージ化する必要があるクラスとリソースファイルを含むディレクトリ。

名前

classesDirectory

タイプ

java.io.File

デフォルト値

${project.build.outputDirectory}

ユーザープロパティ

開始

compilerArguments

AOT コンパイルプロセスに提供する必要がある引数。コマンドラインでは、複数の値を引用符で囲んでください。

名前

compilerArguments

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.aot.compilerArguments

開始

excludeGroupIds

除外する groupId 名のコンマ区切りリスト(完全一致)。

名前

excludeGroupIds

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.excludeGroupIds

開始

1.1.0

excludes

除外するアーティファクト定義のコレクション。Exclude 要素は、必須の groupId および artifactId コンポーネントとオプションの classifier コンポーネントを定義します。プロパティとして構成する場合、値はコンマで区切られ、コンポーネントはコロンで区切られる必要があります: groupId:artifactId,groupId:artifactId:classifier

名前

excludes

タイプ

java.util.List

デフォルト値

ユーザープロパティ

spring-boot.excludes

開始

1.1.0

generatedClasses

生成されたクラスを含むディレクトリ。

名前

generatedClasses

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/main/classes

ユーザープロパティ

開始

generatedResources

生成されたリソースを含むディレクトリ。

名前

generatedResources

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/main/resources

ユーザープロパティ

開始

generatedSources

生成されたソースを含むディレクトリ。

名前

generatedSources

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/main/sources

ユーザープロパティ

開始

includes

含めるアーティファクト定義のコレクション。Include 要素は、必須の groupId および artifactId コンポーネントとオプションの classifier コンポーネントを定義します。プロパティとして構成する場合、値はコンマで区切られ、コンポーネントはコロンで区切られる必要があります: groupId:artifactId,groupId:artifactId:classifier

名前

includes

タイプ

java.util.List

デフォルト値

ユーザープロパティ

spring-boot.includes

開始

1.2.0

jvmArguments

AOT プロセスに関連付ける必要がある JVM 引数。コマンドラインでは、複数の値を引用符で囲んでください。

名前

jvmArguments

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.aot.jvmArguments

開始

mainClass

AOT プロセスのソースとして使用するメインクラスの名前。指定しない場合、'main' メソッドを含む最初に見つかったコンパイル済みクラスが使用されます。

名前

mainClass

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.aot.main-class

開始

profiles

AOT 処理を考慮する Spring プロファイル。

名前

profiles

タイプ

java.lang.String[]

デフォルト値

ユーザープロパティ

開始

skip

実行をスキップします。

名前

skip

タイプ

boolean

デフォルト値

false

ユーザープロパティ

spring-boot.aot.skip

開始

systemPropertyVariables

AOT プロセスに渡す JVM システムプロパティのリスト。

名前

systemPropertyVariables

タイプ

java.util.Map

デフォルト値

ユーザープロパティ

開始

処理テスト

AOT エンジンは、Spring のテストコンテキストフレームワーク を使用する JUnit 5 テストに適用できます。ApplicationContextInitializer コードを生成するために、適切なテストが AOT エンジンによって処理されます。

この機能を使用するようにアプリケーションを構成するには、次の例に示すように、process-test-aot ゴールの実行を追加します。

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>process-test-aot</id>
			<goals>
				<goal>process-test-aot</goal>
			</goals>
		</execution>
	</executions>
</plugin>
spring-boot-starter-parent を使用している場合、nativeTest プロファイルを有効にすると、この実行は自動的に構成されます。

アプリケーション AOT 処理と同様に、BeanFactory はビルド時に完全に準備されます。

spring-boot:process-test-aot

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

テストで AOT エンジンを呼び出します。

必須パラメーター

名前 タイプ デフォルト

classesDirectory

File

${project.build.outputDirectory}

generatedClasses

File

${project.build.directory}/spring-aot/main/classes

generatedResources

File

${project.build.directory}/spring-aot/test/resources

generatedSources

File

${project.build.directory}/spring-aot/test/sources

generatedTestClasses

File

${project.build.directory}/spring-aot/test/classes

testClassesDirectory

File

${project.build.testOutputDirectory}

オプションのパラメーター

名前 タイプ デフォルト

compilerArguments

String

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

skip

boolean

false

systemPropertyVariables

Map

パラメーターの詳細

classesDirectory

テストの実行に使用するクラスとリソースファイルを含むディレクトリ。

名前

classesDirectory

タイプ

java.io.File

デフォルト値

${project.build.outputDirectory}

ユーザープロパティ

開始

compilerArguments

AOT コンパイルプロセスに提供する必要がある引数。コマンドラインでは、複数の値を引用符で囲んでください。

名前

compilerArguments

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.aot.compilerArguments

開始

excludeGroupIds

除外する groupId 名のコンマ区切りリスト(完全一致)。

名前

excludeGroupIds

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.excludeGroupIds

開始

1.1.0

excludes

除外するアーティファクト定義のコレクション。Exclude 要素は、必須の groupId および artifactId コンポーネントとオプションの classifier コンポーネントを定義します。プロパティとして構成する場合、値はコンマで区切られ、コンポーネントはコロンで区切られる必要があります: groupId:artifactId,groupId:artifactId:classifier

名前

excludes

タイプ

java.util.List

デフォルト値

ユーザープロパティ

spring-boot.excludes

開始

1.1.0

generatedClasses

生成されたテストクラスを含むディレクトリ。

名前

generatedClasses

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/main/classes

ユーザープロパティ

開始

generatedResources

生成されたテストリソースを含むディレクトリ。

名前

generatedResources

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/test/resources

ユーザープロパティ

開始

generatedSources

生成されたソースを含むディレクトリ。

名前

generatedSources

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/test/sources

ユーザープロパティ

開始

generatedTestClasses

生成されたテストクラスを含むディレクトリ。

名前

generatedTestClasses

タイプ

java.io.File

デフォルト値

${project.build.directory}/spring-aot/test/classes

ユーザープロパティ

開始

includes

含めるアーティファクト定義のコレクション。Include 要素は、必須の groupId および artifactId コンポーネントとオプションの classifier コンポーネントを定義します。プロパティとして構成する場合、値はコンマで区切られ、コンポーネントはコロンで区切られる必要があります: groupId:artifactId,groupId:artifactId:classifier

名前

includes

タイプ

java.util.List

デフォルト値

ユーザープロパティ

spring-boot.includes

開始

1.2.0

jvmArguments

AOT プロセスに関連付ける必要がある JVM 引数。コマンドラインでは、複数の値を引用符で囲んでください。

名前

jvmArguments

タイプ

java.lang.String

デフォルト値

ユーザープロパティ

spring-boot.aot.jvmArguments

開始

skip

実行をスキップします。

名前

skip

タイプ

boolean

デフォルト値

false

ユーザープロパティ

spring-boot.aot.skip

開始

systemPropertyVariables

AOT プロセスに渡す JVM システムプロパティのリスト。

名前

systemPropertyVariables

タイプ

java.util.Map

デフォルト値

ユーザープロパティ

開始

testClassesDirectory

アーカイブにパッケージ化する必要があるクラスとリソースファイルを含むディレクトリ。

名前

testClassesDirectory

タイプ

java.io.File

デフォルト値

${project.build.testOutputDirectory}

ユーザープロパティ

開始