Maven自动化构建
Maven 的自动化构建主要通过如下两种方案实现:
使用 maven-invoker-plugin 插件。
使用持续集成(CI)服务器自动管理构建自动化,例如 Jenkins (了解即可)。
使用 maven-invoker-plugin 插件
Maven 社区提供了一个名为 maven-invoker-plugin 的插件,该插件能够用来在一组项目上执行构建工作,并检查每个项目是否构建成功,通过它就可以实现 Maven 的自动化构建。如下图所示,在 D:\maven 目录下有 3 个 Maven 项目。

其中,helloMaven 项目的 pom.xml 配置如下。
<project xmlns="http://maven.apache网站站点" rel="nofollow" />
<project xmlns="http://maven.apache网站站点" rel="nofollow" />
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache网站站点" rel="nofollow" />
>mvn help:describe -Dplugin=invoker
Maven 令执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ standalone-pom ---
Downloading from central: https://repo.maven.apache网站站点" rel="nofollow" />
mvn clean install
执行结果如下。
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< net.biancheng.www:helloMaven >--------------------
[INFO] Building helloMaven 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloMaven ---
[INFO] Deleting d:\maven\helloMaven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloMaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory d:\maven\helloMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloMaven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 2 source files to d:\maven\helloMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloMaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory d:\maven\helloMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloMaven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to d:\maven\helloMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloMaven ---
[INFO] Surefire report directory: d:\maven\helloMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloMaven ---
[INFO] Building jar: d:\maven\helloMaven\target\helloMaven-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-invoker-plugin:3.2.2:run (id-integration-test) @ helloMaven ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[WARNING] Filtering of parent/child POMs is not supported without cloning the projects
[INFO] Building: secondMaven\pom.xml
[INFO] secondMaven\pom.xml .............................. SUCCESS (2.6 s)
[INFO] Building: thirdMaven\pom.xml
[INFO] thirdMaven\pom.xml ............................... SUCCESS (3.7 s)
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 2, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloMaven ---
[INFO] Installing d:\maven\helloMaven\target\helloMaven-1.0-SNAPSHOT.jar to D:\myRepository\repository\net\biancheng\www\helloMaven\1.0-SNAPSHOT\helloMaven-1.0-SNAPSHOT.jar
[INFO] Installing d:\maven\helloMaven\pom.xml to D:\myRepository\repository\net\biancheng\www\helloMaven\1.0-SNAPSHOT\helloMaven-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.335 s
[INFO] Finished at: 2021-03-05T10:20:26+08:00
[INFO] ------------------------------------------------------------------------
通过以上执行过程可以看到,在 helloMaven 构建完成后, Invoker 插件对 secondMaven 和 thirdMaven 也进行了构建。
注意:secondMaven 和 thirdMaven 是否依赖于 helloMaven,不会影响 Invoker 插件是否对它们进行构建。即使 secondMaven 和 thirdMaven 都不依赖于 helloMaven,只要在 Invoker 插件中配置了这些项目,Invoker 插件也依然会在 helloMaven 构建完成后,对它们进行构建。