Maven 项目的部署通常包括以下步骤:
(1) 开发人员将项目代码提交到 GIT 或 SVN 等版本控制系统中,并进行标记;
(2) 编译人员从 Git 或 SVN 上下载完整的项目代码;
(3) 构建应用;
(4) 将构建输出的 WAR 或 JAR 文件存放在指定的服务器(Nexus);
(5) 从指定网络中获取文件,并部署到生产环境上;
(6) 更新项目文档以及版本号;
以上这些工作通常由多个不同的团队协作完成,例如,项目研发人员负责项目代码的提交,项目管理员负责项目的构建工作,运维人员负责将构建好的应用文件部署到生产环境上等等。
任何一个环节出现问题,都由可能导致 bug 甚至生产事故的发生,例如,新版本构建完成后,没有发布到指定的网络位置中,导致运维人员再次将旧版本构件部署到生产环境中。
为了避免人员和操作环节出现的问题影响产品的发布,实现自动化部署是一个优化的选择。
1. 自动化部署的实现方式
通常采用如下的架构来管理项目:
(1) 版本控制工具(以 GIT 为例):管理项目源代码
(2) Maven:负责编译和发布项目
(3) 远程仓库管理工具(以 Nexus 为例):管理构建生成的二进制文件(*.jar 或 *.war)
Maven 提供了一个名为 maven-release-plugin 的插件,它可以帮助我们实现 Maven 项目的自动化部署。
maven-release-plugin 提供了很多命令,其中最常用的是以下 4 个:
(1) mvn release:clean
如果上次发布过程不成功,则使用该命令清理工作空间。
(2) mvn release:rollback
若上一次发布过程不成功,回滚对代码和配置所做的更改。
(3) mvn release:prepare
a) 检查是否存在任何未提交的本地更改;
b) 检查是否存在 SNAPSHOT 版本的依赖项;
c) 修改 POM 中的配置,将应用程序的版本从快照版本(SNAPSHOT)修改为正式版本(RELEASE),例如从从 1.0-SNAPSHOT 到 1.0;
d) 针对修改后的 POM 进行测试,确保一切正常;
e) 提交修改后的 POM;
f) 将正式版本的代码提交到 Git,并进行标记;
g) 修改 POM 中的配置,将正式版本升级为更高的快照版本(例如从 1.0 升级到 1.1-SNAPSHOT),以便以后继续使用快照版本进行开发;
h) 将修改后的 POM 提交到 Git;
(4) mvn release:perform
从 Git 中检出上面标记的正式版代码,并执行命令 mvn deploy ,将正式版本的构件部署到 Nexus 中。
2. 使用 maven-release-plugin 插件
本文在 “Maven基础知识(4)- Maven 插件、Maven Archetype (原型/模板)、Maven SNAPSHOT (快照)” 里 MavenDemo03 项目基础上,使用 maven-release-plugin 插件实现项目的自动化部署需要以下步骤。
1) 将 MavenDemo03 上传到 Gitee (或 GitHub) 上,由 Git 管理该项目的代码
Gitee:https://gitee.com/
MavenDemo03 在 Gitee 上的仓库:https://gitee.com/slksm/MavenDemo03.git
注:Git/Gitee 参考相关文档 https://gitee.com/help/categories/43
2) 修改 pom.xml,增加 Git 以及 Nexus 的相关配置如下
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 6 <modelVersion>4.0.0</modelVersion> 7 <groupId>com.example</groupId> 8 <artifactId>MavenDemo03</artifactId> 9 <version>1.0-SNAPSHOT</version>10 <packaging>jar</packaging>11 12 <dependencies>13 <dependency>14 <groupId>junit</groupId>15 <artifactId>junit</artifactId>16 <version>3.8.1</version>17 <scope>compile</scope>18 </dependency>19 </dependencies>20 21 <build>22 <plugins>23 <!-- 配置 maven-release-plugin 插件 -->24 <plugin>25 <groupId>org.apache.maven.plugins</groupId>26 <artifactId>maven-release-plugin</artifactId>27 <version>2.5.3</version>28 <configuration>29 <useReleaseProfile>false</useReleaseProfile>30 <goals>deploy</goals>31 <checkModificationExcludes>32 <checkModificationExclude>.idea</checkModificationExclude>33 </checkModificationExcludes>34 <autoVersionSubmodules>true</autoVersionSubmodules>35 </configuration>36 </plugin>37 </plugins>38 </build>39 40 <!-- 配置 Git 地址 -->41 <scm>42 <url></url>43 <connection>scm:git:https://gitee.com/slksm/MavenDemo03.git</connection>44 <developerConnection>scm:git:https://gitee.com/slksm/MavenDemo03.git</developerConnection>45 </scm>46 47 <!-- Nexus 的地址 -->48 <distributionManagement>49 <repository>50 <id>longsite_release_hosted</id>51 <url>http://localhost:8081/nexus/content/repositories/longsite_release_hosted/</url>52 </repository>53 <snapshotRepository>54 <id>longsite_snapshot_hosted</id>55 <url>http://localhost:8081/nexus/content/repositories/longsite_snapshot_hosted/</url>56 </snapshotRepository>57 </distributionManagement>58 </project>
3) 提交代码
打开 cmd 命令行窗口,进入 D:\Workshop\maven\MavenDemo03 目录,执行如下命令:
D:\Workshop\maven\MavenDemo03>mvn scm:checkin -Dmessage="Test push for deploy"
结果显示如下:
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< com.example:MavenDemo03 >----------------------- [INFO] Building MavenDemo03 1.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-scm-plugin:2.0.0-M1:checkin (default-cli) @ MavenDemo03 --- [INFO] Executing: cmd.exe /X /C "git rev-parse --show-prefix" [INFO] Working directory: D:\Workshop\maven\MavenDemo03 [INFO] Executing: cmd.exe /X /C "git status --porcelain ." [INFO] Working directory: D:\Workshop\maven\MavenDemo03 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.018 s [INFO] Finished at: 2022-06-15T13:42:35+08:00 [INFO] ------------------------------------------------------------------------
4) 发布准备
在 D:\Workshop\maven\MavenDemo03 目录下,执行如下命令:
D:\Workshop\maven\MavenDemo03>mvn release:prepare
结果显示如下:
What is the release version for "MavenDemo03"? (com.example:MavenDemo03) 1.1: :
# 询问 MavenDemo03 项目的发布版本是什么?默认值是 1.1,此处我们直接回车使用默认值。
What is SCM release tag or label for "MavenDemo03"? (com.example:MavenDemo03) MavenDemo03-1.1: :
# 询问 MavenDemo03 项目的 SCM 发布标签是什么?默认值是 MavenDemo03-1.1,此处我们直接回车使用默认值。
What is the new development version for "MavenDemo03"? (com.example:MavenDemo03) 1.2-SNAPSHOT: :
# 询问 MavenDemo03 项目新的开发版本是什么?默认值是 1.2-SNAPSHOT,此处我们直接回车使用默认值。
命令执行完成后,查看该项目的 pom.xml,发现其版本号已经更新为 1.2-SNAPSHOT。
5) 发布到 Nexus
在 D:\Workshop\maven\MavenDemo03 目录下,执行如下命令:
D:\Workshop\maven\MavenDemo03>mvn release:perform
结果显示如下:
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< com.example:MavenDemo03 >----------------------- [INFO] Building MavenDemo03 1.2-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-release-plugin:2.5.3:perform (default-cli) @ MavenDemo03 --- [INFO] Checking out the project to perform the release ... [INFO] Executing: cmd.exe /X /C "git clone --branch MavenDemo03-1.1 https://gitee.com/slksm/MavenDemo03.git D:\Workshop\maven\MavenDemo03\target\checkout" [INFO] Working directory: D:\Workshop\maven\MavenDemo03\target [INFO] Executing: cmd.exe /X /C "git ls-remote https://gitee.com/slksm/MavenDemo03.git" [INFO] Working directory: C:\Users\tkuang\AppData\Local\Temp [INFO] Executing: cmd.exe /X /C "git fetch https://gitee.com/slksm/MavenDemo03.git" [INFO] Working directory: D:\Workshop\maven\MavenDemo03\target\checkout [INFO] Executing: cmd.exe /X /C "git checkout MavenDemo03-1.1" [INFO] Working directory: D:\Workshop\maven\MavenDemo03\target\checkout [INFO] Executing: cmd.exe /X /C "git ls-files" [INFO] Working directory: D:\Workshop\maven\MavenDemo03\target\checkout [INFO] Invoking perform goals in directory D:\Workshop\maven\MavenDemo03\target\checkout [INFO] Executing goals 'deploy'... [WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance. [INFO] [INFO] Scanning for projects... [INFO] [INFO] [INFO] [INFO] ----------------------< com.example:MavenDemo03 >----------------------- [INFO] [INFO] Building MavenDemo03 1.1 [INFO] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo03 --- [INFO] [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] [INFO] Copying 3 resources [INFO] [INFO] [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo03 --- [INFO] [INFO] Changes detected - recompiling the module! [INFO] [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] [INFO] Compiling 1 source file to D:\Workshop\maven\MavenDemo03\target\checkout\target\classes [INFO] [INFO] [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo03 --- [INFO] [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] [INFO] skip non existing resourceDirectory D:\Workshop\maven\MavenDemo03\target\checkout\src\test\resources [INFO] [INFO] [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo03 --- [INFO] [INFO] Changes detected - recompiling the module! [INFO] [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] [INFO] Compiling 1 source file to D:\Workshop\maven\MavenDemo03\target\checkout\target\test-classes [INFO] [INFO] [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo03 --- [INFO] [INFO] Surefire report directory: D:\Workshop\maven\MavenDemo03\target\checkout\target\surefire-reports [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.example.AppTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec [INFO] [INFO] Results : [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] [INFO] [INFO] --- maven-antrun-plugin:1.8:run (default) @ MavenDemo03 --- [INFO] [WARNING] Parameter tasks is deprecated, use target instead [INFO] [INFO] Executing tasks [INFO] [INFO] main: [INFO] [echo] ʹ env.properties临Ƶ classpath [INFO] [copy] Copying 1 file to D:\Workshop\maven\MavenDemo03\target\checkout\target\classes [INFO] [INFO] Executed tasks [INFO] [INFO] [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo03 --- [INFO] [INFO] Building jar: D:\Workshop\maven\MavenDemo03\target\checkout\target\MavenDemo03-1.1.jar [INFO] [INFO] [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo03 --- [INFO] [INFO] Installing D:\Workshop\maven\MavenDemo03\target\checkout\target\MavenDemo03-1.1.jar to C:\Applications\Java\maven-repository\com\example\MavenDemo03\1.1\MavenDemo03-1.1.jar [INFO] [INFO] Installing D:\Workshop\maven\MavenDemo03\target\checkout\pom.xml to C:\Applications\Java\maven-repository\com\example\MavenDemo03\1.1\MavenDemo03-1.1.pom [INFO] [INFO] [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ MavenDemo03 --- [INFO] Uploading to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/1.1/MavenDemo03-1.1.jar [INFO] Progress (1): 3.6 kB [INFO] [INFO] Uploaded to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/1.1/MavenDemo03-1.1.jar (3.6 kB at 14 kB/s) [INFO] Uploading to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/1.1/MavenDemo03-1.1.pom [INFO] Progress (1): 4.1/6.5 kB [INFO] Progress (1): 6.5 kB [INFO] [INFO] Uploaded to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/1.1/MavenDemo03-1.1.pom (6.5 kB at 35 kB/s) [INFO] Downloading from longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/maven-metadata.xml [INFO] Uploading to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/maven-metadata.xml [INFO] Progress (1): 298 B [INFO] [INFO] Uploaded to longsite_release_hosted: http://localhost:8081/nexus/content/repositories/longsite_release_hosted/com/example/MavenDemo03/maven-metadata.xml (298 B at 1.2 kB/s) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [INFO] BUILD SUCCESS [INFO] [INFO] ------------------------------------------------------------------------ [INFO] [INFO] Total time: 7.494 s [INFO] [INFO] Finished at: 2022-06-15T14:10:03+08:00 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Cleaning up after release... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 17.541 s [INFO] Finished at: 2022-06-15T14:10:03+08:00 [INFO] ------------------------------------------------------------------------
6) 查看 Nexus 上 MavenDemo03 项目的状态
访问 Nexus 用户界面,查看仓库列表,选中名称为 Release_host 的宿主仓库,点击 Browse Index 选项卡,查看该仓库中的构件。我们会发现 MavenDemo03-1.1.jar (本文发布 1.0 时因配置原因上传 nexus 失败)已经被部署到 Nexus 仓库中,如下图。
注意事项:
(1) 运行命令前,一定要将所有代码都提交到 Git,不然会报错。
(2) 当发布完成后,项目的版本号会自动更新,并且将正式版发布到指定的 Release 库中。
转自:https://www.cnblogs.com/tkuang/p/16378414.html