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 build output WAR or EAR file in a common network location.
- Fetch the file from the network and deploy it to the production site.
- Update the documentation and the application's version number.
Description
Typically, the development process mentioned above involves multiple teams. One team may be responsible for committing code, another for building, and so on. Due to manual operations and a multi-team environment, any step is likely to go wrong. For example, an older version was not updated on the network machine, and then the deployment team redeployed an earlier build.
Solution
Automated deployment is achieved by combining the following approaches:
- Use Maven to build and release the project
- Use SubVersion, a source code repository, to manage source code
- Use remote repository management software (Jfrog or Nexus) to manage project binaries.
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 of 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
<
connectionscm: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
<
nameRelease 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
projectIn the pom.xml file, some important element nodes we commonly use are shown in the table below:
| element nodes | Description |
|---|---|
| SCM | Configure the SVN path from which Maven will check out the code. |
| repository | The location of the built WAR, EAR, or JAR file, or the storage location of other artifacts generated after a successful source build. |
| Plugin | Configure the maven-release-plugin to implement the automated deployment process. |
Maven Release Plugin
Maven uses the maven-release-plugin to accomplish 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 proceeds successfully.
mvn release:prepare
Perform various operations:
- Check whether there are uncommitted changes locally
- Ensure there are no snapshot dependencies
- Change the application's version information for the release
- Update the POM file to SVN
- Run test cases
- Commit the modified POM file
- Tag the code in SVN
- Increment the version number and append a snapshot for future releases
- Commit the modified POM file to SVN
mvn release:perform
Switch the code to the previously tagged location and run the Maven deploy goal to deploy the WAR file or build the corresponding structure into the repository.
Open a command terminal, go to the C:\ > MVN >bus-core-api directory, and execute the following mvn command.
C:\MVN\bus-core-api>mvn release:prepare
Maven starts building the whole project. After a successful build, run the following mvn command.
C:\MVN\bus-core-api>mvn release:perform
After a successful build, you can verify that the JAR file uploaded to your repository is effective.