如何在Spring Boot Native Image插件构建镜像时传递参数与环境变量?
Spring Boot Native Image插件构建镜像时配置ARG与环境变量的方法
设置环境变量
Spring Boot Maven插件的spring-boot:build-image目标支持在pom.xml中直接配置镜像的环境变量,通过<environment>节点定义:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <!-- 配置镜像环境变量 --> <environment> <SPRING_PROFILES_ACTIVE>prod</SPRING_PROFILES_ACTIVE> <APP_NAME>native-demo</APP_NAME> </environment> </image> </configuration> </plugin>
如果需要临时指定环境变量,也可以通过命令行参数传递:
mvn -Pnative spring-boot:build-image -Dspring-boot.build-image.environment.SPRING_PROFILES_ACTIVE=test
设置构建参数(ARG)
构建时的ARG参数可以通过<buildArgs>节点配置,这些参数仅在镜像构建阶段生效:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <!-- 配置构建阶段的ARG参数 --> <buildArgs> <BASE_JVM_VERSION>21</BASE_JVM_VERSION> <BUILD_OPTS>--enable-preview</BUILD_OPTS> </buildArgs> </image> </configuration> </plugin>
同样,临时构建参数也支持命令行传递:
mvn -Pnative spring-boot:build-image -Dspring-boot.build-image.buildArgs.BASE_JVM_VERSION=22
注意:Spring Boot默认使用Paketo Buildpacks进行镜像构建,部分通用ARG参数(如基础镜像版本、构建选项)会被Buildpacks识别并处理。
内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ
相关产品推荐
相关产品推荐

