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:

env.jdbc.url=jdbc:mysql://mysql.test.com:3306/myschema?useUnicode=true

env.domain=test.hongliang.com

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

env.jdbc.url=jdbc:mysql://mysql.prod.com:3306/myschema?useUnicode=true

env.domain=prod.hongliang.com

 

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">

        <constructor-arg>

            <map>

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

            ……                  

            </map>

        </constructor-arg>

    </bean>

 

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.

 

<build>

    <filters>

        <filter>src/main/filters/filter-${env}.properties</filter>

    </filters>

    <sourceDirectory>src/main/java</sourceDirectory>

    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>

    <testSourceDirectory>src/test/java</testSourceDirectory>

    <directory>target</directory>

    <outputDirectory>target/classes</outputDirectory>

    <testOutputDirectory>target/test-classes</testOutputDirectory>

    <finalName>${artifactId}</finalName>

    <resources>

        <resource>

            <directory>src/main/resources</directory>

            <filtering>true</filtering>*

            <excludes>

                <exclude>context.xml</exclude>

            </excludes>

        </resource>

        <resource>

            <directory>src/main/resources/META-INF</directory>

            <filtering>true</filtering>

            <targetPath>../${artifactId}/META-INF</targetPath>

            <includes>

                <include>context.xml</include>

            </includes>

        </resource>

    </resources>

    <testResources>

        <testResource>

            <directory>src/test/resources</directory>

            <filtering>true</filtering>

        </testResource>

</testResources>

</build>

 

*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

http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

 

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

<filter>src/main/filters/filter-${env}.properties</filter>

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 –>

    <properties>

        <env>dev</env>

    </properties>

    <!– profiles –>

    <profiles>

        <profile>

            <id>dev</id>

            <properties>

                <env>dev</env>

            </properties>

        </profile>

        <profile>

            <id>test</id>

            <properties>

                <env>test</env>

            </properties>

        </profile>

    </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

 

Alternatives

In
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
file.

 

<Context>

<Parameter
name="
env.domain" value=" prod.hongliang.com "/>

</Context>

 

Reference:

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

http://sujitpal.blogspot.com/2006/10/maven2-multi-environment-filter-setup.html

 

 

Advertisements
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