Gradle build spring boot app as war with active profile
Asked Answered
E

1

7

I would like to package my spring boot application as war for a specific profile. That can be done with setting spring.profiles.active=profile name in application.properties file. Is it possible to set it as a parameter when building war eg. gradle build --spring.profiles.active=profile name?

Elohist answered 16/7, 2014 at 14:12 Comment(1)
You can pass parameter to gradle build using -P switch. Then refer to passed property with project instance.Indefectible
R
12

It sounds like you want to bake the value of spring.profiles.active into the war when it's built. You can do so using Gradle's support for resource filtering. Configure your application.properties like this:

spring.profiles.active=@activeProfiles@

And then apply filtering in your build.gradle to replace @activeProfiles@ with the value of a property:

processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [
        activeProfiles: activeProfiles
    ]
}

In this example, I've used a property named activeProfiles. You'd then have to supply a value when you run the build:

./gradlew -PactiveProfiles=foo build
Reprieve answered 16/7, 2014 at 15:30 Comment(9)
Andy, this solution does work for me. But, the problem is that when I switch the profile, the processResources is not being executed again.Dean
Perhaps grade's skipping the task as it incorrectly believes it's up-to-date? Try using clean as well: ./gradlew -PactiveProfiles=switched clean buildReprieve
I used this approach in one of my projects and I had an issue with binary files being filtered which caused that e.g. fonts were not properly displayed in the application after the deployment. Process resources task, as defined here, processes all files which may lead that binary files are corrupted in the end. The solution is to limit the filtered files to those that really needs to be filtered. I described this in this blog post in more details: blog.codeleak.pl/2015/1...Pilau
This doesn't work for me. I see an error: Could not get unknown property 'activeProfiles' for task ':processResources' of type org.gradle.language.jvm.tasks.ProcessResources.Mulford
That error means that you haven’t set the activeProfiles property. How did you try to set it?Reprieve
@AndyWilkinson - I was not setting the property activeProfiles properly. Modified it and it worked like a charm. Thanks.Mulford
This works fine for gradle build. But how to set default active profile while running the project from IDE directly?Amen
@JigneshM.Khatri Probably best dealt with as a separate question as it depends on exactly what you want to do. One approach would be to configure your IDE to launch the app with spring.profiles.active=yourProfile set as a system property.Reprieve
@AndyWilkinson Here I have opened a separate question - #66050982 , may be you can answer there.Amen

© 2022 - 2024 — McMap. All rights reserved.