How to set spring active profiles with maven profiles How to set spring active profiles with maven profiles spring spring

How to set spring active profiles with maven profiles


There is a more elegant way to switch between 2 maven+spring profiles simultaneously.

First, add profiles to POM (pay attention - maven+spring profile is activated by single system variable):

<profiles>    <profile>        <id>postgres</id>        <activation>            <activeByDefault>true</activeByDefault>            <property>                <name>spring.profiles.active</name>                <value>postgres</value>            </property>        </activation>        <dependencies>            <dependency>                <groupId>postgresql</groupId>                <artifactId>postgresql</artifactId>                <version>9.1-901.jdbc4</version>            </dependency>        </dependencies>    </profile>    <profile>        <id>h2</id>        <activation>            <property>                <name>spring.profiles.active</name>                <value>h2</value>            </property>        </activation>                   <dependencies>            <dependency>                <groupId>com.h2database</groupId>                <artifactId>h2</artifactId>                <version>1.4.191</version>            </dependency>        </dependencies>    </profile></profiles>

Second, set default profile for spring (for maven it is already set in POM). For web application, I inserted following lines to web.xml:

<context-param>   <param-name>spring.profiles.default</param-name>   <param-value>postgres</param-value></context-param>

Third, add profile-dependent beans to your config. In my case (XML config), it is:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">    <property name="dataSource" ref="mainDataSource" />    <property name="jpaVendorAdapter">        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />    </property>    <property name="jpaProperties" ref="hibProps"/>    <property name="packagesToScan">        <list>            <value>my.test.model</value>        </list>    </property></bean>...<beans profile="postgres">    <bean name="mainDataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="org.postgresql.Driver" />        <property name="url" value="jdbc:postgresql://127.0.0.1:5432/webchat" />        <property name="username" value="postgres" />        <property name="password" value="postgres" />    </bean></beans><beans profile="h2">    <bean name="mainDataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="org.h2.Driver" />        <property name="url" value="jdbc:h2:file:./newsdb;INIT=RUNSCRIPT FROM 'classpath:init.sql';TRACE_LEVEL_FILE=0" />        <property name="username" value="sa" />        <property name="password" value="" />    </bean></beans>

Now it is possible to:

  • Run my web-app on Postgres DB with mvn jetty:run or mvn jetty:run -Dspring.profiles.active=postgres commands
  • Run my web-app on H2 DB with mvn clean jetty:run -Dspring.profiles.active=h2


The first thing you need is two properties files for keeping your configurations. The names of the files should match with the pattern application-{custom_suffix}.properties. Create them in the src/main/resources directory of your Maven project, next to the main application.properties file, which you’re going to use later to activate one of the others and to hold values shared by both profiles.

Then it’s time to modify your pom.xml. You need to define a custom property in each of your Maven profiles and set their values to match with suffixes of corresponding properties files that you want to load with a particular profile. The following sample also marks the first profile to run by default, but it’s not mandatory.

<profile>    <id>dev</id>    <properties>        <activatedProperties>dev</activatedProperties>    </properties>    <activation>        <activeByDefault>true</activeByDefault>    </activation></profile><profile>    <id>release</id>    <properties>        <activatedProperties>release</activatedProperties>    </properties></profile>

Next, in the build section of the same file, configure filtering for the Resources Plugin. That will allow you to insert properties defined in the previous step into any file in the resources directory, which is the subsequent step.

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

Finally, add the following line to the application.properties.

spring.profiles.active=@activatedProperties@

When the build is run, the Resources Plugin will replace the placeholder with the value of the property defined in the active Maven profile. After starting your application, the Spring framework will load the appropriate configuration file based on the name of the active Spring profile, which is described by the value of the spring.profiles.active property. Note that Spring Boot 1.3 replaced the default Resources Plugin syntax for filtered values and uses @activatedProperties@ instead of ${activatedProperties} notation.

It worked to perfection. Hope this can help you.


You'll have to filter a resources of your application, for instance a property file, that holds the information of which profile to activate in spring.

For instance

spring.profile = ${mySpringProfile}

And for each profile, define a value for this variable (mySpringProfile).

During the build, this will be filtered accordingly to the value defined in the currently active profile.

Then during the bootstrap of your application you'll select the appropriated profile according to this file (can't help you more as you didn't gave us more information, but this is quite easy.

Note: I can't find a way to get the currently active profile in maven (something like project.profiles.active that holds your -P values), that's why you'll have to set a new variable for each profile.

Note 2: if you are running a web application, instead of using this intermediate file, filter this value in your web.xml

<context-param>    <param-name>spring.profiles.active</param-name>    <param-value>${mySpringProfile}</param-value></context-param>

Note 3: This is actually a bad practice, and you should set the profile at runtime with a system property