Gradle构建Spring Boot应用程序与有效配置文件进行战争

Gradle build spring boot app as war with active profile

我想将我的spring boot应用程序打包为针对特定配置文件的war。 这可以通过在application.properties文件中设置spring.profiles.active = profile名称来完成。
进行战争时是否可以将其设置为参数。 gradle build --spring.profiles.active =配置文件名称?


听起来好像您想在构建spring.profiles.active的值时将其烘烤。 您可以使用Gradle的资源过滤支持来做到这一点。 像这样配置您的application.properties

1
spring.profiles.active=@activeProfiles@

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

1
2
3
4
5
processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        activeProfiles: activeProfiles
    ]
}

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

1
./gradlew -PactiveProfiles=foo build