If you use Spring boot, this is an easy way to do this.
Create two profiles in maven and set a property in each profile with the name of the Spring profile that you want to execute.
<profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> <environment>develop</environment> </properties> </profile>
Inside the application.properties application, add this property:
spring.profiles.active = $ {spring.profiles.active}
- Create application.property for each profile using this application-profile.properties template. For instance:
application-dev.properties
application-prod.properties
- Necessarily active filtering in the resource plugin:
... <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> ...
Another way is to create a maven runtime file called activeprofile.properties . Spring boot looks at this file to load the active profile. You can create this file as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>prepare-package</phase> <configuration> <target> <echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" /> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> <configuration> </configuration> </plugin>
source share