Tuesday, September 1, 2015

Using Profiles in Maven

One of Maven's many advantages is the ability to configure automated deployments.

 By configuring the Maven POM, we can set a Web Archive (WAR) deployments to a given JEE Server (e.g. Tomcat, Liberty Core, etc).

Most projects involving more than one developer have some concept of a deployment path. A deployment artifact is likely deployed on a local web server (DEV), then deployed on an integration server (INT), and promoted to a test server (QA) if a smoke test passes.

We can configure each environment in the deployment path within our POM file.

Typically, the POM file for a Tomcat deployment would contain a section that looks something like this:
<plugin>  
     <groupId>org.apache.tomcat.maven</groupId>  
     <artifactId>tomcat7-maven-plugin</artifactId>  
     <version>2.2</version>  
     <configuration>  
          <url>http://localhost:8080/manager/text</url>  
          <server>TomcatServer</server>  
          <path>/test</path>  
          <username>craig</username>  
          <password>password</password>  
     </configuration>  
</plugin>  

And the WAR artifact is deployed using this command:
$ mvn tomcat7:deploy

This works great until we need to manage multiple environments.

Rather than maintaining multiple <configuration> sections, and commenting out the current one that is not in use, we can do something like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.myproject</groupId>
 <artifactId>myproject</artifactId>
 <version>1.0.0</version>
 <packaging>war</packaging>

 <url />
 <name>My Project</name>
 <inceptionYear>2015</inceptionYear>
 <description>A Description Here</description>

 <build>
  <plugins>

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <inherited>true</inherited>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>

   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>

   <plugin>
          <artifactId>maven-war-plugin</artifactId>
         <configuration>
           <webResources>
                   <resource>
                <!-- this is relative to the pom.xml directory -->
                 <directory>../preDeploy</directory>
                  </resource>
           </webResources>
          </configuration>
    </plugin> 

   <!-- Required for Maven-to-Tomcat Deployments -->
   <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>      
     <path>/mypath</path>
     <url>${deploy_url}</url>
     <server>${deploy_server}</server> 
     <username>${deploy_username}</username> 
     <password>${deploy_password}</password>
    </configuration>
   </plugin>

  </plugins>
 </build>

 <!-- Build with -P<profileName> to activate one of the profiles-->
 <profiles>
  <profile>
   <id>local</id>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
    <deploy_url>http://192.168.1.200:8080/manager/text</deploy_url>
    <deploy_server>TomcatServer</deploy_server>
    <deploy_username>craig</deploy_username>
    <deploy_password>tomcat</deploy_password>
   </properties>
   <build>
    <resources>
     <resource>
      <directory>src/main/resources/local</directory>
     </resource>
    </resources>
   </build>
  </profile>
  <profile>
   <id>int</id>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
    <deploy_url>http://192.168.1.200:8081/manager/text</deploy_url>
    <deploy_server>TomcatServer</deploy_server>
    <deploy_username>craig</deploy_username>
    <deploy_password>password</deploy_password>
   </properties>
   <build>
    <resources>
     <resource>
      <directory>src/main/resources/local</directory>
     </resource>
    </resources>
   </build>
  </profile>
  <profile>
   <id>qa</id>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
   <properties>
    <deploy_url>http://192.168.1.200:8082/manager/text</deploy_url>
    <deploy_server>TomcatServer</deploy_server>
    <deploy_username>craig</deploy_username>
    <deploy_password>tomcat</deploy_password>
   </properties>
   <build>
    <resources>
     <resource>
      <directory>src/main/resources/local</directory>
     </resource>
    </resources>
   </build>
  </profile>
 </profiles>

 <repositories>
  <repository>
   <id>maven2-repository.java.net</id>
   <name>Java.net Repository for Maven</name>
   <url>http://download.java.net/maven/2/</url>
   <layout>default</layout>
  </repository>
 </repositories>

 <dependencies>
  <!-- 
   dependencies are listed here 
  -->
 </dependencies>

</project>
I've included the entire POM for reference.  The relevant section is under the <profiles /> element.

And the WAR artifact is deployed using this command:
$ mvn tomcat7:deploy -Pdev

The text highlighted in red above corresponds to the profile name that we have defined in the POM.



References

  1. [Blogger] Build Automation with Tomcat and Maven
    1. other articles on Maven

2 comments:

  1. Excellent Blog very imperative good content, this article is useful to beginners and real time Employees. DevOps Online Training

    ReplyDelete