在Maven构建中传递Spring配置文件

14

我正在通过以下方式传递Spring活动配置文件来运行Spring Boot应用:

spring-boot:run -Dspring.profiles.active=dev

我如何在使用maven创建包时传递spring.profiles.active参数?以下是maven命令:

mvn clean install

2
mvn clean install -Dspring.profiles.active=dev ? 请将上述代码翻译成中文。 - Tunaki
1
不,那不起作用。当我运行war时,它说配置文件未设置。 - Ronald Randon
然后您需要发布一些代码。从POM开始。 - Tunaki
您想使用Maven在构建时设置系统属性(-Dspring.profiles.active=dev),以便Spring可以使用该属性,是吗? - Saïd
更多信息请参见:https://riptutorial.com/spring-boot/example/31100/set-the-right-spring-profile-by-building-the-application-automatically--maven- - gonella
4个回答

10
如果有人遇到相同情况,可以通过使用Spring Boot和Maven来完成以下两个步骤: 首先,在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>

接着运行:

mvn clean install -Pdev

6

Maven是一个构建工具,它唯一能够修改构建后运行时行为的方式是使用它的(构建时间)配置文件。这不应与Spring的运行时配置文件混淆,后者是指令,指导Spring容器以特定方式启动应用程序。

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


1

对于包,您可以将安装替换为包

mvn clean package -P dev


0

您可以使用环境变量。

export SPRING_PROFILES_ACTIVE=some,test,profiles
mvn spring-boot:run

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接