C7 – Maven profile for multiple environments deployment

Maven profiles for multiple environments deployment

In case that you need to deploy your application on to multiple environments for different purposes, such as one for test  and one for production, you might need different configuration respectively (ie link to different database). It would be really troublesome to change those configurations each time you want to compile and deploy onto a different server. Maven filter is really handy there to help you out of those headaches


Profile file

You need to write the configurations into a property file respectively for each environment that you want to deploy. For example:



Let’s save this file as filter-test.properties for the moment and we create another file as filter-prod.properties with following contents




Using configuration variable in your application

You can apply the variables you defined earlier in any of your resource files, such as an IoC configuration file.


    <bean id="configParameters" class="java.util.HashMap" lazy-init="true">



                  <entry><key><value>DOMAIN_NAME</value></key><value>${env.domain} </value></entry>






POM file

After you have done the preceding steps, you need to modify your pom.xml to tell maven which configuration it should look when you ask it to build the application.






































*You may also notice the <filtering> tag in the code above. Please be aware it has nothing to do with selecting environment profile. More details about this could be found with the following URL



As you can tell, the following code indicates that variable ${env} is deciding which profile that maven should look into


Question is where the ${env} is defined and how to tell Maven which option we want to adopt.


Profile definition

You profiles should be defined in the POM file. You have to define the default environment and all other profiles respectively

    <!– default environment –>




    <!– profiles –>
















Maven command to build application with adopted profile

-P in Maven command line is used to indicate the active profile you want to use with this build. If no profile indicate, Maven will pick up the default one.


mvn install –P test



favour of using servlet context parameters to store environment specific
configuration, you can specify the parameters on each server. For example, if
you are using tomcat servers, you could add the following code in the
context.xml file on the production tomcat server instead of writing a profile



env.domain" value=" prod.hongliang.com "/>








This entry was posted in Maven. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s