Build Profile
Often our project needs to build for a particular environment like public cloud, docker, kubernetes or developer’s local machine. Each environment may require special treatment like specific dependencies, environment variables etc. Even our project CI/CD may require additional configurations than our developers local machine. Gradle build profile is here to rescue. We can declare Maven like profile using gradle and design environment specific configuration for our project build.
Release date: January 2021 | Spring Boot: 2.4.x | Revision: 1
Action: Define environment specific build profile
Create Profile
Here we initially create dev
build profile.
Initially we will include gradle dependencies caching configuration at our dev profile.
-
Create a
profile-dev.gradle
file atgradle
directory. -
Add bellow snippet at the dev profile.
profile-dev.gradleconfigurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' (1) }
1 This configuration will always reload dependencies during gradle refresh. It is very helpful for referencing internal library snapshots and library development. -
Add profile switching logic to root
build.gradle
file.build.gradleif (!hasProperty('buildProfile')) ext.buildProfile = 'dev' (1) if (file("gradle/profile-${buildProfile}.gradle").exists()) { (2) apply from: "gradle/profile-${buildProfile}.gradle" (3) }
1 If buildProfile
property absent from build command we explicitly setdev
profile.2 Validating if mentioned build profile is available. 3 Applying build profile if it exists. -
To activate profile during project build.
gradlew clean build -PbuildProfile=prod (1) gradlew clean build (2)
1 To activate explicitly build profile 2 Build with the defaults.
You may want to learn to link your gradle build profile with your spring boot profile. To learn the process see. |
Learn More
-
Official Doc - Maven Like Gradle Profiles
Project Code
-
Github - Build Profile