ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Maven基础知识(5)- Maven 自动化构建

2022-06-09 11:33:26  阅读:196  来源: 互联网

标签:INFO maven plugin 基础知识 Maven invoker MavenDemo01 自动化


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

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

    (1) 使用 maven-invoker-plugin 插件。
    (2) 使用持续集成(CI)服务器自动管理构建自动化,例如 Jenkins(Hudson)。

1. 使用 maven-invoker-plugin 插件

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

    前文使用的示例 MavenDemo02 依赖于 MavenDemo01,当 MavenDemo01 发生变化时,要保证 MavenDemo02 使用最新的 MavenDemo01,所以在 MavenDemo01 项目里使用 maven-invoker-plugin 插件添加一个编译目标来提醒 MavenDemo02。

        注:MavenDemo01 见 “Maven基础知识(1)- Maven 简介、Maven 安装配置、创建 Quickstart 项目”,MavenDemo02 见 “Maven基础知识(2)- Maven 坐标、Maven 外部依赖、Maven 仓库

    示例,这里我们将在 MavenDemo01 项目里使用 maven-invoker-plugin 插件,实现 MavenDemo01 提醒 MavenDemo02 完成自动构建。

    1) MavenDemo01 项目的 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
 4                                     http://maven.apache.org/maven-v4_0_0.xsd">
 5 
 6             <modelVersion>4.0.0</modelVersion>
 7             <groupId>com.example</groupId>
 8             <artifactId>MavenDemo01</artifactId>
 9             <packaging>jar</packaging>
10             <version>1.0-SNAPSHOT</version>
11             <name>MavenDemo01</name>
12             <url>http://maven.apache.org</url>
13             <dependencies>
14                 <dependency>
15                 <groupId>junit</groupId>
16                 <artifactId>junit</artifactId>
17                 <version>3.8.1</version>
18                 <scope>test</scope>
19                 </dependency>
20             </dependencies>
21 
22             <build>
23                 <plugins>
24                     <plugin>
25                         <groupId>org.apache.maven.plugins</groupId>
26                         <artifactId>maven-invoker-plugin</artifactId>
27                         <version>3.2.2</version>
28                         <configuration>
29                             <debug>true</debug>
30                             <projectsDirectory>D:\Workshop\maven</projectsDirectory>
31                             <pomIncludes>
32                                 <pomInclude>MavenDemo02\pom.xml</pomInclude>
33                             </pomIncludes>
34                         </configuration>
35                         <executions>
36                             <execution>
37                                 <id>id-integration-test</id>
38                                 <goals>
39                                     <goal>run</goal>
40                                 </goals>
41                             </execution>
42                         </executions>
43                     </plugin>
44                 </plugins>
45             </build>     
46         </project>


        以上配置中,在 build 的 plugins 子元素中使用了一个 plugin 元素声明了一个构建期的插件 maven-invoker-plugin,该插件配置中各元素含义如下:
            
            (1) groupId:插件的项目组 id;
            (2) artifactId:插件的项目或模块 id;
            (3) version:插件的版本;
            (4) projectsDirectory:需要构建项目的目录,该元素可单独使用,表示该目录下的所有 Maven 项目都会在当前项目构建完成后开始构建;
            (5) pomIncludes:该元素内可以声明一个或多个 pomInclude 元素,需与 projectDirectory 元素配合使用,共同指定需要构建项目的 pom.xml。

    2) MavenDemo02 项目的 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
 4                                     http://maven.apache.org/maven-v4_0_0.xsd">
 5 
 6             <modelVersion>4.0.0</modelVersion>
 7             <groupId>com.example</groupId>
 8             <artifactId>MavenDemo02</artifactId>
 9             <packaging>jar</packaging>
10             <version>1.0-SNAPSHOT</version>
11             <name>MavenDemo02</name>
12             <url>http://maven.apache.org</url>
13             <dependencies>
14                 <dependency>
15                 <groupId>junit</groupId>
16                 <artifactId>junit</artifactId>
17                 <version>3.8.1</version>
18                 <scope>test</scope>
19                 </dependency>
20                 
21                 <dependency>
22                     <groupId>com.example</groupId>
23                     <artifactId>MavenDemo01</artifactId>
24                     <scope>system</scope>
25                     <version>1.0-SNAPSHOT</version>
26                     <systemPath>D:\Workshop\maven\MavenDemo01\target\MavenDemo01-1.0-SNAPSHOT.jar</systemPath>
27                 </dependency>    
28             </dependencies>
29 
30             <build>
31                 <plugins>
32                     <!--添加 site 插件-->
33                     <plugin>
34                         <groupId>org.apache.maven.plugins</groupId>
35                         <artifactId>maven-site-plugin</artifactId>
36                         <version>3.7.1</version>
37                     </plugin>
38                 </plugins>
39             </build>  
40         </project>


    3) 自动化构建

        打开 cmd 命令行窗口,进入 D:\Workshop\maven 目录,执行如下命令:

            D:\Workshop\maven>mvn help:describe -Dplugin=invoker

        显示 maven-invoker-plugin 插件绑定的目标的信息:

            [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 ---
            [INFO] org.apache.maven.plugins:maven-invoker-plugin:3.3.0

            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.3.0
            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:  1.575 s
            [INFO] Finished at: 2022-06-06T11:38:11+08:00
            [INFO] ------------------------------------------------------------------------


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


        进入 D:\Workshop\maven\MavenDemo01 目录,执行如下命令:

            D:\Workshop\maven\MavenDemo01>mvn clean install

        显示结果如下:

            [INFO] Scanning for projects...
            [INFO]
            [INFO] ----------------------< com.example:MavenDemo01 >-----------------------
            [INFO] Building MavenDemo01 1.0-SNAPSHOT
            [INFO] --------------------------------[ jar ]---------------------------------
            [INFO]
            [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo01 ---
            [INFO] Deleting D:\Workshop\maven\MavenDemo01\target
            [INFO]
            [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo01 ---
            [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
            [INFO] skip non existing resourceDirectory D:\Workshop\maven\MavenDemo01\src\main\resources
            [INFO]
            [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo01 ---
            [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:\Workshop\maven\MavenDemo01\target\classes
            [INFO]
            [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MavenDemo01 ---
            [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
            [INFO] skip non existing resourceDirectory D:\Workshop\maven\MavenDemo01\src\test\resources
            [INFO]
            [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MavenDemo01 ---
            [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:\Workshop\maven\MavenDemo01\target\test-classes
            [INFO]
            [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MavenDemo01 ---
            [INFO] Surefire report directory: D:\Workshop\maven\MavenDemo01\target\surefire-reports

            -------------------------------------------------------
            T E S T S
            -------------------------------------------------------
            Running com.example.AppTest
            Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

            Results :

            Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

            [INFO]
            [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MavenDemo01 ---
            [INFO] Building jar: D:\Workshop\maven\MavenDemo01\target\MavenDemo01-1.0-SNAPSHOT.jar
            [INFO]
            [INFO] --- maven-invoker-plugin:3.2.2:run (id-integration-test) @ MavenDemo01 ---
            [INFO] Building: MavenDemo02\pom.xml
            [INFO]           MavenDemo02\pom.xml .............................. SUCCESS (5.7 s)
            [INFO] -------------------------------------------------
            [INFO] Build Summary:
            [INFO]   Passed: 1, Failed: 0, Errors: 0, Skipped: 0
            [INFO] -------------------------------------------------
            [INFO]
            [INFO] --- maven-install-plugin:2.4:install (default-install) @ MavenDemo01 ---
            [INFO] Installing D:\Workshop\maven\MavenDemo01\target\MavenDemo01-1.0-SNAPSHOT.jar to C:\Applications\Java\maven-repository\com\example\MavenDemo01\1.0-SNAPSHOT\MavenDemo01-1.0-SNAPSHOT.jar
            [INFO] Installing D:\Workshop\maven\MavenDemo01\pom.xml to C:\Applications\Java\maven-repository\com\example\MavenDemo01\1.0-SNAPSHOT\MavenDemo01-1.0-SNAPSHOT.pom
            [INFO] ------------------------------------------------------------------------
            [INFO] BUILD SUCCESS
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time:  16.798 s
            [INFO] Finished at: 2022-06-06T11:34:56+08:00
            [INFO] ------------------------------------------------------------------------


        通过以上执行过程可以看到,在 MavenDemo01 构建完成后, Invoker 插件对 MavenDemo02 也进行了构建。

        注:MavenDemo02 是否依赖于 MavenDemo01,不会影响 Invoker 插件是否对它们进行构建。即使 MavenDemo02 都不依赖于 MavenDemo01,只要在 Invoker 插件中配置了这些项目,Invoker 插件也依然会在 MavenDemo01 构建完成后,对它们进行构建。


2. 使用持续集成(CI)服务器自动管理构建自动化

    Jenkins 是一个开源的、提供友好操作界面的持续集成 CI(Continuous integration,持续集成)工具,起源于 Hudson(Hudson 是商用的),主要用于持续、自动的构建/测试软件项目、监控外部任务的运行。Jenkins 用 Java 语言编写,可在 Tomcat 等流行的 Servlet容器中运行,也可独立运行。

    Jenkins 通常与版本管理工具 (SCM)、构建工具结合使用。常用的版本控制工具有 GIT、SVN 等,构建工具有 Maven、Ant、Gradle 等。

    Jenkins:https://www.jenkins.io/
    Jenkins GitHub:https://github.com/jenkinsci/jenkins

    假设我们使用 GIT 管理 MavenDemo01 和 MavenDemo02 的项目代码,而且项目代码对应的 GIT 仓库在 Jenkins 做了映射。

    在 Jenkins 上配置一个 Task,如果 MavenDemo01 的项目代码发生变化,Task 将会在指定时间或周期性的,自动启动 MavenDemo01 的构建工作,完成 MavenDemo01 的构建后,Task 再自动启动构建指定的(或查找到)依赖于 MavenDemo01 的相关项目(这里指 MavenDemo02)。


标签:INFO,maven,plugin,基础知识,Maven,invoker,MavenDemo01,自动化
来源: https://www.cnblogs.com/tkuang/p/16358676.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有