You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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ỹ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:03:16