Here's a detailed guide to setting up Maven profiles to manage Spring profiles, complete with necessary changes to your pom.xml and application.properties.
Step 1: Define Maven Profiles in pom.xml
Add the following Maven profiles to your pom.xml. This configuration will allow you to switch between different profiles (dev and release) and set the dev profile to be active by default.
Step 2: Enable Resource Filtering
If you are not using spring-boot-starter-parent as the parent of your pom.xml, you need to enable resource filtering for the Resources Plugin. Add the following section to the <build> section of your pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory> <filtering>true</filtering>
</resource>
</resources>
<!-- other build configurations -->
</build>
If you are using spring-boot-starter-parent, you can skip this step.
Step 3: Modify application.properties
In your src/main/resources/application.properties file, add the following line:
spring.profiles.active=@activeProperties@
The @activatedProperties@ placeholder will be replaced by the property value from the active Maven profile during the build process.
How It Works
When you run the Maven build with a specific profile, the Resources Plugin will replace the @activatedProperties@ placeholder in application.properties with the value from the active Maven profile.
For example:
If you run mvn clean install, the dev profile will be active by default, and spring.profiles.active will be set to dev.
If you run mvn clean install -P release, the release profile will be active, and spring.profiles.active will be set to release.
Example of Running Maven Commands
To build with the default profile (dev):
mvn clean install
To build with the release profile:
mvn clean install -P release
Summary
By following these steps, you create a dynamic link between Maven and Spring profiles. This setup enables you to switch between different configurations effortlessly, ensuring that your Spring application runs with the correct settings for each environment.
Commentaires