Maven 简明教程

Maven - Deployment Automation

在项目开发中,一般情况下,部署流程包含以下步骤:

In project development, normally a deployment process consists of the following steps −

  1. Check-in the code from all project in progress into the SVN (version control system) or source code repository and tag it.

  2. Download the complete source code from SVN.

  3. Build the application.

  4. Store the build output either WAR or EAR file to a common network location.

  5. Get the file from network and deploy the file to the production site.

  6. Updated the documentation with date and updated version number of the application.

Problem Statement

一般情况下,上述部署流程涉及多人。一个团队可能处理代码检入,另一个团队可能处理构建,以此类推。很容易出现任何步骤因需要人工操作而遗漏,也可能是由于多团队环境所致。例如,可能未在网络计算机上替换旧的构建,而部署团队再次部署了旧构建。

There are normally multiple people involved in the above mentioned deployment process. One team may handle check-in of code, other may handle build and so on. It is very likely that any step may be missed out due to manual efforts involved and owing to multi-team environment. For example, older build may not be replaced on network machine and deployment team deployed the older build again.

Solution

通过结合以下内容来自动部署流程:

Automate the deployment process by combining the following −

  1. Maven, to build and release projects.

  2. SubVersion, source code repository, to manage source code.

  3. Remote Repository Manager (Jfrog/Nexus) to manage project binaries.

Update Project POM.xml

我们将使用 Maven Release 插件来创建一个自动化发布过程。

We will be using Maven Release plug-in to create an automated release process.

例如:bus-core-api 项目 POM.xml。

For Example: bus-core-api project POM.xml.

<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>

在 Pom.xml 中,我们使用了以下重要的元素:

In Pom.xml, following are the important elements we have used −

Sr.No.

Element & Description

1

SCM Configures the SVN location from where Maven will check out the source code.

2

Repositories Location where built WAR/EAR/JAR or any other artifact will be stored after code build is successful.

3

Plugin maven-release-plugin is configured to automate the deployment process.

Maven Release Plug-in

Maven 使用 maven-release-plugin 执行以下有用的任务。

The Maven does the following useful tasks using maven-release-plugin.

mvn release:clean

如果上次发布过程未成功,则清除工作空间。

It cleans the workspace in case the last release process was not successful.

mvn release:rollback

如果上次发布过程未成功,则回滚对工作空间代码和配置所做的更改。

Rollback the changes done to workspace code and configuration in case the last release process was not successful.

mvn release:prepare

执行多个操作,例如:

Performs multiple number of operations, such as −

  1. Checks whether there are any uncommitted local changes or not.

  2. Ensures that there are no SNAPSHOT dependencies.

  3. Changes the version of the application and removes SNAPSHOT from the version to make release.

  4. Update pom files to SVN.

  5. Run test cases.

  6. Commit the modified POM files.

  7. Tag the code in subversion

  8. Increment the version number and append SNAPSHOT for future release.

  9. Commit the modified POM files to SVN.

mvn release:perform

使用先前定义的标记签出代码,并运行 Maven deply 目标,将 war 或构建的制品部署到存储库。

Checks out the code using the previously defined tag and run the Maven deploy goal, to deploy the war or built artifact to repository.

让我们打开命令控制台,转到 C:\ > MVN >bus-core-api 目录并执行以下 mvn 命令。

Let’s open the command console, go to the C:\ > MVN >bus-core-api directory and execute the following mvn command.

>mvn release:prepare

Maven 将开始构建项目。成功构建后,运行以下 mvn 命令。

Maven will start building the project. Once build is successful run the following mvn command.

>mvn release:perform

成功构建后,您可以在存储库中验证上传的 JAR 文件。

Once build is successful you can verify the uploaded JAR file in your repository.