Maven Automated Deployment

During project development, the deployment process includes the following steps:

  • Commit all project code to SVN or a code repository and tag it.
  • Download the complete source code from SVN.
  • Build the application.
  • Store the built WAR or EAR files to a common network location.
  • Retrieve the files from the network and deploy them to the production site.
  • Update documentation and update the application version number.

Problem Description

Usually, the development process mentioned above involves multiple teams. One team may be responsible for committing code, another team for building, etc. It is very likely that any step can go wrong due to the manual operations and multi-team environment involved. For example, an older version is not updated on the network machine, and then the deployment team redeploys an earlier build version.

Solution

Achieve automated deployment by combining the following approaches:

  • Use Maven to build and release projects
  • Use SubVersion, the source repository, to manage source code
  • Use remote repository management software (Jfrog or Nexus) to manage project binary files.

Modify the project's pom.xml

We will use the Maven Release plugin to create an automated release process.

For example, the pom.xml file for the bus-core-api project is as follows:

<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>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <scm> <url>http://www.svn.com</url> <connection>scm:svn:http://localhost:8080/svn/jrepo/trunk/ Framework</connection> <developerConnection>scm:svn:${username}/${password}@localhost:8080: common_core_api:1101:code</developerConnection> </scm> <distributionManagement> <repository> <id>Core-API-Java-Release</id> <name>Release repository</name> <url>http://localhost:8081/nexus/content/repositories/ Core-Api-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>[bus-core-api-release-checkin]-< /scmCommentPrefix> </configuration> </plugin> </plugins> </build> </project>

Some important element nodes commonly used in the pom.xml file are shown in the table below:

Element Node Description
SCM Configure the SVN path from which Maven will fetch the code.
repository The location of the built WAR, EAR, or JAR files, or the storage location of other artifacts generated after a successful source build.
Plugin Configure the maven-release-plugin plugin to implement the automated deployment process.

Maven Release Plugin

Maven uses the maven-release-plugin plugin to complete the following tasks.

mvn release:clean

Clean the workspace to ensure the latest release process proceeds successfully.

mvn release:rollback

If the previous release process was unsuccessful, roll back the modified workspace code and configuration to ensure the release process succeeds.

mvn release:prepare

Perform various operations:

  • Check whether there are uncommitted modifications locally
  • Ensure there are no snapshot dependencies
  • Change the application's version information for release
  • Update POM files to SVN
  • Run test cases
  • Commit the modified POM files
  • Tag the code on SVN
  • Increment the version number and append a snapshot for future releases
  • Commit the modified POM files to SVN
mvn release:perform

Switch the code to the previously tagged location, run the Maven deploy goal to deploy the WAR file or build the corresponding artifacts into the repository.

Open a command terminal, navigate to the C:\ > MVN >bus-core-api directory, and then execute the following mvn command.

C:\MVN\bus-core-api>mvn release:prepare

Maven starts building the entire project. After a successful build, you can run the following mvn command.

C:\MVN\bus-core-api>mvn release:perform

After a successful build, you can verify whether the uploaded JAR file in your repository is effective.

Other Extensions