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?
Asked
Active
Viewed 6,599 times
7
-
You can pass parameter to gradle build using `-P` switch. Then refer to passed property with project instance. – Opal Jul 16 '14 at 14:16
1 Answers
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

Andy Wilkinson
- 108,729
- 24
- 257
- 242
-
Andy, this solution does work for me. But, the problem is that when I switch the profile, the processResources is not being executed again. – rakpan Jul 05 '15 at 02:57
-
Perhaps grade's skipping the task as it incorrectly believes it's up-to-date? Try using `clean` as well: `./gradlew -PactiveProfiles=switched clean build` – Andy Wilkinson Jul 05 '15 at 07:36
-
1I 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: http://blog.codeleak.pl/2015/1... – Rafal Borowiec Nov 28 '15 at 21:53
-
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. – Tora Tora Tora Dec 08 '18 at 18:09
-
That error means that you haven’t set the `activeProfiles` property. How did you try to set it? – Andy Wilkinson Dec 08 '18 at 18:57
-
@AndyWilkinson - I was not setting the property `activeProfiles` properly. Modified it and it worked like a charm. Thanks. – Tora Tora Tora Dec 08 '18 at 22:39
-
This works fine for gradle build. But how to set default active profile while running the project from IDE directly? – Jignesh M. Khatri Feb 03 '21 at 17:29
-
@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. – Andy Wilkinson Feb 03 '21 at 18:06
-
@AndyWilkinson Here I have opened a separate question - https://stackoverflow.com/questions/66050482/manage-spring-boot-profiles-with-gradle , may be you can answer there. – Jignesh M. Khatri Feb 04 '21 at 17:16