Maven自动化构建

Maven 自动化构建是一种方案,即当某个项目构建完成后(特别是有代码更新的情况下),所有依赖它的相关项目也应该开始构建过程,以确保这些项目的稳定运行。

Maven 的自动化构建主要通过如下两种方案实现:

  • 使用 maven-invoker-plugin 插件。
  • 使用持续集成(CI)服务器自动管理构建自动化,例如 Jenkins (了解即可)。

使用 maven-invoker-plugin 插件

Maven 社区提供了一个名为 maven-invoker-plugin 的插件,该插件能够用来在一组项目上执行构建工作,并检查每个项目是否构建成功,通过它就可以实现 Maven 的自动化构建。

如下图所示,在 D:\maven 目录下有 3 个 Maven 项目。

Maven 构建自动化图1

其中,helloMaven 项目的 pom.xml 配置如下。

  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 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.jhelp.www</groupId>
  6. <artifactId>helloMaven</artifactId>
  7. <packaging>jar</packaging>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>helloMaven</name>
  10. <url>http://maven.apache.org</url>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>3.8.1</version>
  16. <scope>test</scope>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <plugins>
  21. <!-- 添加invoker 插件 -->
  22. <plugin>
  23. <groupId>org.apache.maven.plugins</groupId>
  24. <artifactId>maven-invoker-plugin</artifactId>
  25. <version>3.2.2</version>
  26. <configuration>
  27. <debug>true</debug>
  28. <!--设置 invkoer插件 添加的 pom 文件所在的文件夹 -->
  29. <projectsDirectory>D:\maven</projectsDirectory>
  30. <!-- 设置 invkoer插件 添加的 pom 文件 -->
  31. <pomIncludes>
  32. <pomInclude>secondMaven\pom.xml</pomInclude>
  33. <pomInclude>thirdMaven\pom.xml</pomInclude>
  34. </pomIncludes>
  35. </configuration>
  36. <executions>
  37. <execution>
  38. <id>id-integration-test</id>
  39. <!-- 执行的目标 -->
  40. <goals>
  41. <goal>run</goal>
  42. </goals>
  43. </execution>
  44. </executions>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

以上配置中,在 build 的 plugins 子元素中使用了一个 plugin 元素声明了一个构建期的插件 maven-invoker-plugin,该插件配置中各元素含义如下:

  • groupId:插件的项目组 id;
  • artifactId:插件的项目或模块 id;
  • version:插件的版本;
  • projectsDirectory:需要构建项目的目录,该元素可单独使用,表示该目录下的所有 Maven 项目都会在当前项目构建完成后开始构建;
  • pomIncludes:该元素内可以声明一个或多个 pomInclude 元素,需与 projectDirectory 元素配合使用,共同指定需要构建项目的 pom.xml。

secondMaven 项目依赖于 helloMaven ,其 pom.xml 配置如下。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>net.jhelp.www</groupId>
  5. <artifactId>secondMaven</artifactId>
  6. <packaging>jar</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>secondMaven</name>
  9. <url>http://maven.apache.org</url>
  10. <build>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-site-plugin</artifactId>
  15. <version>3.7.1</version>
  16. </plugin>
  17. <plugin>
  18. <groupId>org.apache.maven.plugins</groupId>
  19. <artifactId>maven-project-info-reports-plugin</artifactId>
  20. <version>3.0.0</version>
  21. </plugin>
  22. </plugins>
  23. </build>
  24. <dependencies>
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <artifactId>junit</artifactId>
  28. <version>3.8.1</version>
  29. <scope>test</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>net.jhelp.www</groupId>
  33. <artifactId>helloMaven</artifactId>
  34. <scope>system</scope>
  35. <version>1.0-SNAPSHOT</version>
  36. <systemPath>D:\maven\helloMaven\target\helloMaven-1.0-SNAPSHOT.jar</systemPath>
  37. </dependency>
  38. </dependencies>
  39. </project>

thirdMaven 项目依赖于 helloMaven ,其 pom.xml 配置如下。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.jhelp.www</groupId>
  6. <artifactId>thirdMaven</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>thirdMaven</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>net.jhelp.www</groupId>
  25. <artifactId>helloMaven</artifactId>
  26. <version>1.0-SNAPSHOT</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <pluginManagement>
  31. <plugins>
  32. <plugin>
  33. <artifactId>maven-clean-plugin</artifactId>
  34. <version>3.1.0</version>
  35. </plugin>
  36. <plugin>
  37. <artifactId>maven-resources-plugin</artifactId>
  38. <version>3.0.2</version>
  39. </plugin>
  40. <plugin>
  41. <artifactId>maven-compiler-plugin</artifactId>
  42. <version>3.8.0</version>
  43. </plugin>
  44. <plugin>
  45. <artifactId>maven-surefire-plugin</artifactId>
  46. <version>2.22.1</version>
  47. </plugin>
  48. <plugin>
  49. <artifactId>maven-jar-plugin</artifactId>
  50. <version>3.0.2</version>
  51. </plugin>
  52. <plugin>
  53. <artifactId>maven-install-plugin</artifactId>
  54. <version>2.5.2</version>
  55. </plugin>
  56. <plugin>
  57. <artifactId>maven-deploy-plugin</artifactId>
  58. <version>2.8.2</version>
  59. </plugin>
  60. <plugin>
  61. <artifactId>maven-site-plugin</artifactId>
  62. <version>3.7.1</version>
  63. </plugin>
  64. <plugin>
  65. <artifactId>maven-project-info-reports-plugin</artifactId>
  66. <version>3.0.0</version>
  67. </plugin>
  68. </plugins>
  69. </pluginManagement>
  70. </build>
  71. </project>

打开命令控制台,执行如下命令,查看 maven-invoker-plugin 插件绑定的目标。

  1. >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.org/maven2/org/apache/maven/plugins/maven-invoker-plugin/maven-metadata.xml
  • Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-invoker-plugin/maven-metadata.xml (888 B at 539 B/s)
  • [INFO] org.apache.maven.plugins:maven-invoker-plugin:3.2.2
  • Name: Apache Maven Invoker Plugin
  • Description: The Maven Invoker Plugin is used to run a set of Maven projects.
  • The plugin can determine whether each project execution is successful, and
  • optionally can verify the output generated from a given project execution.
  • Group Id: org.apache.maven.plugins
  • Artifact Id: maven-invoker-plugin
  • Version: 3.2.2
  • Goal Prefix: invoker
  • This plugin has 6 goals:
  • invoker:help
  • Description: Display help information on maven-invoker-plugin.
  • Call mvn invoker:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  • details.
  • invoker:install
  • Description: Installs the project artifacts of the main build into the
  • local repository as a preparation to run the sub projects. More precisely,
  • all artifacts of the project itself, all its locally reachable parent POMs
  • and all its dependencies from the reactor will be installed to the local
  • repository.
  • invoker:integration-test
  • Description: Searches for integration test Maven projects, and executes
  • each, collecting a log in the project directory, will never fail the build,
  • designed to be used in conjunction with the verify mojo.
  • invoker:report
  • Description: Generate a report based on the results of the Maven
  • invocations. Note: This mojo doesn't fork any lifecycle, if you have a
  • clean working copy, you have to use a command like mvn clean
  • integration-test site to ensure the build results are present when this
  • goal is invoked.
  • Note: This goal should be used as a Maven report.
  • invoker:run
  • Description: Searches for integration test Maven projects, and executes
  • each, collecting a log in the project directory, and outputting the results
  • to the command line.
  • invoker:verify
  • Description: Checks the results of maven-invoker-plugin based integration
  • tests and fails the build if any tests failed.
  • For more information, run 'mvn help:describe [...] -Ddetail'
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 4.467 s
  • [INFO] Finished at: 2021-03-05T10:03:15+08:00
  • [INFO] ------------------------------------------------------------------------

由以上执行结果可知,maven-invoker-plugin 插件绑定的 Maven 生命周期阶段为 integration-test 及其以后,所以执行 integration-test 阶段及其以后的都可以触发该插件。

跳转到 D:\maven\helloMaven 目录下,执行如下 Maven 命令。

  • mvn clean install

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] --------------------< net.jhelp.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.jhelp.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\jhelp\www\helloMaven\1.0-SNAPSHOT\helloMaven-1.0-SNAPSHOT.jar
  • [INFO] Installing d:\maven\helloMaven\pom.xml to D:\myRepository\repository\net\jhelp\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 构建完成后,对它们进行构建。

腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/AfJhXy8mKIMNe500 (转载请保留)。
关注下面的标签,发现更多相似文章