Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify alternative Spring application.properties in Maven Pom

Tags:

spring

maven

How would I go about specifying an alternative application.properties in a Maven pom file? I have 2 different Maven profiles and would like to use different a different property file depending on the profile.

like image 564
Fredrik L Avatar asked Mar 12 '15 22:03

Fredrik L


1 Answers

If you are using Spring boot, there is an easy way of doing this.

  1. Create two profiles in maven, and set a property in each profile with the name of the Spring profile 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>
    
  2. Inside your application.properties, add this property:

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

  1. Create an application.property for each profile, using this pattern application-profile.properties. For example:

application-dev.properties
application-prod.properties

  1. Be sure to active filtering in the resource plugin:

      ...
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
     ...

Another way is to create a file during the execution of maven called activeprofile.properties. Spring boot looks 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>
like image 96
jfcorugedo Avatar answered Oct 11 '22 05:10

jfcorugedo