See the Tutorial List

Maven Deployment Automation

In the previous tutorial, 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="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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.webproject</groupId>
  <artifactId>SampleWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0 </version>
  <name>SampleWebApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <scm>
    <url>http://www.svn.com</url>
    <connection>scm:svn:http://localhost:8080/svn/jrepo/trunk/Framework</connection>
    <developerConnection>
     	scm:svn:test/test123@localhost:8080:common_core_api:1101:code
    </developerConnection>
  </scm>
   
  <distributionManagement>
    <repository>
      <id>Sample-Web-App-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>maven-release-plugin</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>junit</groupId>
      <artifactId>junit</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 ElementDescription
scmConfigures the scm (in this cases SVN is used) of the project
repositoriesStorage location of the WAR/EAR/JAR file after the successful build
pluginmaven-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.