How to use Maven and various application.properties for artifact generation of different testing regions in a Spring configured app

continuous-integration, java, maven, spring

Solution

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

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>

Inside your application.properties, add this property: spring.profiles.active=${spring.profiles.active}

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

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>

Problem

I want to use Maven to handle artifact generation for the different local and testing regions. I believe I can use different profiles but I am not certain. In Maven can I select different directories to select files used on packaging (such as application.properties)? How would I set that up? An idea of what I want is to have a the following folders for resources in my project - local - build server - dev - sys - prod Each folder should contain a different version of application.resources which is a file in Spring that can be used to handle hard-coded strings for use in variables. For local builds- our developers also work on different operating systems. Should I require I want to make it seamless on different OS' also. Key outcomes would be: - Control Maven lifecycle phases from inside the IDE (IntelliJ) - Not complicate phases and team processes - Keep things as consistent for each developer - Make the different configurations per developer/region appear invisible when running a phase e.g. install Ideally I would have my project set up according to best practices (Duvall, Matyas, Glover).

Original source