Specify alternative Spring application.properties in Maven Pom

How can I specify an alternative application.properties in the Maven pom file? I have two different Maven profiles and you want to use different property files depending on the profile.

+5
source share
1 answer

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> <!-- Default spring profile to use --> <spring.profiles.active>dev</spring.profiles.active> <!-- Default environment --> <environment>develop</environment> </properties> </profile> 
  • Inside the application.properties application, add this property:

spring.profiles.active = $ {spring.profiles.active}

  1. Create application.property for each profile using this application-profile.properties template. For instance:

application-dev.properties
application-prod.properties

  1. 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> 
+5
source

Source: https://habr.com/ru/post/1215264/


All Articles