通过Maven构建中的Spring配置文件

Ron*_*don 5 java maven spring-boot

我通过传递spring活动配置文件来运行spring boot应用程序,如下所示:

spring-boot:run -Dspring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud)

但是在使用maven创建包时如何传递spring.profiles.active。以下是maven命令:

mvn clean install
Run Code Online (Sandbox Code Playgroud)

Alz*_*eem 9

如果有人遇到同样的情况,您可以通过 spring boot 和 maven 2 个步骤来实现:首先在 spring 属性或 yaml 文件中,添加 spring.profiles.active 并将其值作为占位符:

spring.profiles.active=@active.profile@

其次,用maven传递值:

mvn clean install -Dactive.profile=dev

当 jar/war 打包时,该值将设置为 dev。

您还可以利用 maven 配置文件的使用:

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <active.profile>dev</active.profile>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <active.profile>test</active.profile>
            </properties>
        </profile>
    </profiles>
Run Code Online (Sandbox Code Playgroud)

然后运行:

mvn clean install -Pdev
Run Code Online (Sandbox Code Playgroud)


kry*_*ger 6

Maven 是一个构建时工具,使其修改构建工件的最终运行时行为的唯一方法是使用其(构建时)配置文件。这不能与 Spring 的运行时配置文件混淆,后者是指示 Spring 容器以特定方式引导应用程序的参数。

换句话说,该spring.profiles.active参数不会被 maven “烘焙到”war 文件中,您仍然需要在启动应用程序时传递它,无论是通过命令行参数或配置文件还是您的 servlet 容器的任何机制优惠。