English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Maven Automated Deployment

The deployment process during the project development process includes the following steps:

  • Submit the project code to SVN or the code repository and tag it.

  • Download the complete source code from SVN.

  • Build the application.

  • Store the WAR or EAR files generated by the build in a commonly used network location.

  • Download files from the network and deploy files to the production site.

  • Update the documentation and update the application version number.

Problem Description

In most cases, the development process mentioned above involves multiple teams. One team may be responsible for submitting code, while another team may be responsible for building, etc. It is very likely that any step may fail due to human error and the multi-team environment. For example, the older version was not updated on the network machine, and then the deployment team redeployed an earlier build version.

Solution

To achieve automated deployment, the following solutions can be combined:

  • Use Maven to build and release the project

  • Use SubVersion, source code 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 plugin published by Maven to create an automated release process.

For example, bus-core-api The pom.xml file code of the 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>

The following table shows some important element nodes that we often use in the pom.xml file:

Element nodeDescription
SCMConfigure the SVN path, Maven will download the code from this path.
repositoryThe location of the built WAR or EAR or JAR file, or the storage location of the components generated after the source code is successfully built.
PluginConfigure maven-release-Use the plugin plugin to implement the automated deployment process.

Maven Release Plugin

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

mvn release:clean

Clean the workspace to ensure the successful execution of the latest release process.

mvn release:rollback

In case the last release process was not successful, rollback the modified workspace code and configuration to ensure the release process goes smoothly.

mvn release:prepare

Execute multiple operations:

  • Check if there are any uncommitted changes locally

  • Ensure there are no snapshot dependencies

  • Change the application version information for release

  • Update the POM file to SVN

  • Run test cases

  • Commit the modified POM file

  • Mark the code on SVN for the changes

  • Increase the version number and add a snapshot for future release

  • Commit the modified POM file to SVN

mvn release:perform

Switch the code to the place you marked before, run the Maven deployment goal to deploy the WAR file or build the corresponding structure into the repository.

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

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

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

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

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