Gradle构建spring boot应用程序作为战争与活动配置文件

sta*_*mis 5 gradle spring-profiles spring-boot

我想将我的spring boot应用程序打包为特定配置文件的战争.这可以通过在application.properties文件中设置spring.profiles.active = profile name来完成.是否有可能在建立战争时将其设置为参数,例如.gradle build --spring.profiles.active = 个人资料名称

And*_*son 9

这听起来像你想要spring.profiles.active在战争结束时烘焙战争的价值.您可以使用Gradle对资源过滤的支持来完成此操作.配置你application.properties这样:

spring.profiles.active=@activeProfiles@
Run Code Online (Sandbox Code Playgroud)

然后在您的应用中过滤build.gradle以替换@activeProfiles@属性的值:

processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        activeProfiles: activeProfiles
    ]
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我使用了一个名为的属性activeProfiles.然后,您必须在运行构建时提供值:

./gradlew -PactiveProfiles=foo build
Run Code Online (Sandbox Code Playgroud)