我在这个线程中回答了同样的问题:Pass Spring profile in maven build
,但我将在这里再次重复答案。
如果有人遇到同样的情况,要使用特定配置文件运行 spring boot 可运行 jar 或 war,您需要spring.profiles.active
在默认 application.properties 文件中提供该属性,以便在生成工件时动态更改其值,您可以这样做:
首先在 spring 属性或 yaml 文件中,添加 spring.profiles.active 并将其值作为占位符:
spring.profiles.active=@active.profile@
二、用maven传值:
mvn clean package spring-boot:repackage -Dactive.profile=dev
或者如果你的 pom 中已经出现了 spring-boot 插件,如下所示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
您可以改为运行以下命令:
mvn clean package -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>prod</active.profile>
</properties>
</profile>
</profiles>
然后运行:
mvn clean install -Pdev
您不需要传递 2 个属性文件(默认和 dev/prod),默认情况下 application.properties 中的变量将首先执行。