使用spring-boot,我知道我可以拥有配置文件,并根据活动的配置文件使用不同的配置文件。例如命令:
"mvn spring-boot:运行-Drun.profiles=default,生产“
我将使用" application -default.properties“和"application-production.properties”中定义的设置来运行我的spring-boot应用程序,其中第二个文件中的设置将覆盖第一个文件中定义的相同设置(例如数据库连接设置)。所有这些目前都运行良好。
但是,我想要构建我的spring-boot应用程序,并使用以下命令生成一个可运行的jar:
"mvn package spring-boot:repackage“。
这个命令可以很好地生成自包含的、可运行的jar。问题是,我如何使用前一个命令指定活动的配置文件?我用过
mvn包spring-boot:重新打包-Drun.profiles=default,生产
但它不起作用。
发布于 2019-03-31 07:47:13
我在这篇文章中回答了同样的问题:Pass Spring profile in maven build ,但我在这里再重复一遍。
如果有人有同样的情况,要使用特定的配置文件运行spring boot runnable jar或war,您需要在默认的application.properties文件中提供属性spring.profiles.active,要在生成工件时动态更改其值,您可以这样做:
首先,在spring属性或yaml文件中,添加spring.profiles.active及其值作为占位符:
spring.profiles.active=@active.profile@
其次,使用maven传递该值:
mvn clean package spring-boot:repackage -Dactive.profile=dev或者如果spring-boot插件已经出现在你的pom中,如下所示:
<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您不需要传递两个属性文件(默认和开发/生产),默认情况下,将首先执行application.properties中的变量。
发布于 2016-03-21 03:02:58
spring概要文件的目标是应用程序运行时。它们不会像Maven一样在打包应用程序时运行。因此,您必须在启动应用程序时使用它们,而不是在打包应用程序时。
但是,如果您想生成不同的包,每个包都有一些默认配置文件,那么可以尝试Maven资源过滤。毕竟,使用Maven构建Spring Boot runnable jar的方法是遵循标准过程,因此需要使用Spring Boot Maven插件:
mvn clean install -PproductionMvnProfile另请参阅:
https://stackoverflow.com/questions/36117973
复制相似问题