MAVEN Tutorial

Maven Tutorials – Maven Deployment Automation

Previous – Chapter 14

Maven Tutorial – Chapter 15

Maven Deployment Automation

In the previous chapter, we learnt about creating a simple web project by using the maven archetype and also understood how maven organizes the project structure based on the selected archetype. In order to test the web application created by the maven, we followed the manual way of deploying the application (war file) into the web server and tested the same.

In a real time project, a typical deployment process involves the below list of activities:

  • Checking in the code to the repository.
  • Download the source code from the repository.
  • Compile and build the application and generate jar/war from the same.
  • Locate the generated war/jar in a common shared network location.
  • Download the jar/war file and deploy to the target server
  • Also, documentation and versioning details of the application needs to be update.

Since, any real time project will always involve multiple teams working for the above set of activities, sometimes a particular step may be missed out and which would result in the failure of the build and deployment process. Also, it is observed practically that manually handling the above steps will be error prone most of the times. To overcome this issue, the deployment process should be automated so there will not be any intervention and also the deployment is successful.

How to automate the deployment process?

Basically, to automate the build & release process in maven, release plugin of the maven will be used. Open the pom.xml of the project and update as shown below:

<project xmlns=<em>"http://maven.apache.org/POM/4.0.0"</em> xmlns:xsi=<em>"http://www.w3.org/2001/XMLSchema-instance"</em>
   xsi:schemaLocation=<em>"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</em>>

<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.webproject</groupId>
<artifactId>SampleWebApp</artifactId>
<packaging>war</packaging>
<version>1.0 </version>
<name>SampleWebApp <span style="text-decoration: underline;">Maven</span> <span style="text-decoration: underline;">Webapp</span></name>
<url>http://maven.apache.org</url>
<scm>
     <url>http://www.svn.com</url>
     <connection>scm:svn:http://<span style="text-decoration: underline;">localhost</span>:8080/<span style="text-decoration: underline;">svn</span>/<span style="text-decoration: underline;">jrepo</span>/trunk/Framework</connection>
     <developerConnection>scm:svn:test/test123<span style="text-decoration: underline;">@localhost</span>:8080:common_core_api:1101:code</developerConnection>
</scm>

<distributionManagement>
     <repository>
         <id>Sample-Web-<span style="text-decoration: underline;">App</span>-Release</id>
         <name>Release repository</name>
         <url>http://localhost:8082/nexus/content/repositories/Sample-Web-App-Release</url>
     </repository>
</distributionManagement>

<build>
     <plugins>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId><span style="text-decoration: underline;">maven</span>-release-<span style="text-decoration: underline;">plugin</span></artifactId>
           <version>2.0-beta-9</version>
           <configuration>
               <useReleaseProfile>false</useReleaseProfile>
               <goals>deploy</goals>
               <scmCommentPrefix>[Sample-Web-App-checkin]</scmCommentPrefix>
           </configuration>
         </plugin>
     </plugins>
   </build>

<dependencies>
   <dependency>
     <groupId><span style="text-decoration: underline;">junit</span></groupId>
     <artifactId><span style="text-decoration: underline;">junit</span></artifactId>
     <version>3.8.1</version>
     <scope>test</scope>
   </dependency>
</dependencies>
</project>

Have a closer look at the pom.xml and observe the below list of important tags used:

Tag Element Description
scm Configures the scm (in this cases SVN is used) of the project
repositories Storage location of the WAR/EAR/JAR file after the successful build
plugin maven-release-plugin to automate the process

How does maven release plugin works?

With the updated pom.xml, we all set to automate the deployment process of a project in maven. But, before to conclude this, it is vital to understand what actually happens in the background that makes the deployment process as automated.

When maven invokes the plugin maven-release-plugin, the below tasks are executed accordingly:

mvn release:clean – cleans up the workspace for the last build and sets up for a fresh build.

mvn release:rollback – if the last process was unsuccessful, it reverts/rollback the workspace.

mvn release:prepare – This tasks performs the below list of operations

  • Checks out for any uncommitted files in the local workspace.
  • Checks and ensures for no SNAPSHOT dependencies.
  • Prepares the Release version.
  • Updates the pom to SCM (SVN/Git/Mercurial/CVS)
  • Executes the test cases.
  • Performs the final commit to the SCM.
  • Tags the code.
  • Increments the version no. and adds the SNAPSHOT as part of the future releases.

mvn release:perform – checks out the code from the repository and runs the maven goal to build and deploy the artifacts to the repository.

 Next – Chapter 16


Comments

Questions? Comments? Suggestions? Let us know!! Like / Subscribe / Follow for more updates.