zoukankan      html  css  js  c++  java
  • Maven 教程(15)— 实现多个项目关联自动化构建(maven-invoker-plugin插件的使用)

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79726664

    一、场景
    设想一个团队正在开发一个项目 bus-core-api,并且有其他两个项目 app-web-ui 和 app-desktop-ui 依赖于这个项目。

    bus-core-api 项目为 1.0 快照版本。

    app-web-ui 项目使用的是 bus-core-api 项目的 1.0 快照。

    app-desktop-ui 项目使用的是 bus-core-api 项目的 1.0 快照。

    现在 app-web-ui 和 app-desktop-ui 项目的团队要求的是不管 bus-core-api 项目何时变化,他们的构建过程都应当可以启动。

    使用快照确保了最新的 bus-core-api 项目会被使用,但要达到上面的要求,我们还需要做一些额外的工作。

    提示:其实这个场景有一点矛盾,但是为了演示效果,可以这样理解,即当 bus-core-api 项目构建时,自动构建 app-web-ui 和 app-desktop-ui 项目。

    二、构建方式选择
    有以下三种构建方式:

    在 bus-core-api 项目的 pom.xml 文件中添加一个 maven-invoker-plugin 插件操作来启动 app-web-ui 和 app-desktop-ui 项目的构建;
    使用持续集成(CI) 服务器,比如Jenkins,来自行管理构建自动化(省略);
    使用脚本实现(Linux/Windows)(省略)。
    三、使用 maven-invoker-plugin 插件操作实现详解

    源码地址:https://gitee.com/liupeifeng3514/maven_automation_construction
    maven-invoker-plugin插件详细用法参考:http://maven.apache.org/plugins/maven-invoker-plugin/
    
    准备环境:
    
    建立目录 C:Maven-Build-Automation 和 C:Maven-Build-Automationprojects;
    在 C:Maven-Build-Automation 下 创建 bus-core-api 项目,在 C:Maven-Build-Automationprojects 下创建 app-web-ui 和 app-desktop-ui 项目。
    目录结构如下:
    ├─bus-core-api
    │  ├─src
    │  │  ├─main
    │  │  │  └─java
    │  │  └─test
    │  │      └─java
    │  └─target
    │      ├─classes
    │      ├─invoker-reports
    │      ├─maven-archiver
    │      ├─surefire-reports
    │      └─test-classes
    └─projects
        ├─app-desktop-ui
        │  ├─src
        │  │  ├─main
        │  │  │  └─java
        │  │  └─test
        │  │      └─java
        │  └─target
        │      ├─classes
        │      ├─maven-archiver
        │      ├─surefire
        │      ├─surefire-reports
        │      └─test-classes
        └─app-web-ui
            ├─src
            │  ├─main
            │  │  └─java
            │  └─test
            │      └─java
            └─target
                ├─classes
                ├─maven-archiver
                ├─surefire
                ├─surefire-reports
                └─test-classes

    app-web-ui项目的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>app-web-ui</groupId>
        <artifactId>app-web-ui</artifactId>
        <version>1.0</version>
        <name>网页 UI</name>
        <description>app-web-ui 依赖 bus-core-api</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>bus-core-api</groupId>
                <artifactId>bus-core-api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <scope>system</scope>
                <systemPath>C:Maven-Build-Automationus-core-api	argetus-core-api-0.0.1-SNAPSHOT.jar</systemPath>
            </dependency>
        </dependencies>
    </project>
    提示:为了测试,设置 bus-core-api 项目依赖为本地依赖。其中C:Maven-Build-Automationus-core-api	argetus-core-api-1.0-SNAPSHOT.jar为
    bus-core-api项目生成的jar包最终存放位置。

    app-desktop-ui项目的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>app-desktop-ui</groupId>
        <artifactId>app-desktop-ui</artifactId>
        <version>1.0</version>
        <name>应用程序桌面 UI</name>
        <description>app-desktop-ui 依赖 bus-core-api</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>bus-core-api</groupId>
                <artifactId>bus-core-api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <scope>system</scope>
                <systemPath>C:Maven-Build-Automationus-core-api	argetus-core-api-0.0.1-SNAPSHOT.jar</systemPath>
            </dependency>
        </dependencies>
    </project>

    bus-core-api项目的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>0.0.1-SNAPSHOT</version>
        <name>总线核心 Api</name>
        <description>总线核心 Api</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-invoker-plugin</artifactId>
                    <version>2.0.0</version>
                    <configuration>
                        <debug>true</debug>
                        <projectsDirectory>C:Maven-Build-Automationprojects</projectsDirectory>
                        <cloneProjectsTo>${project.build.directory}</cloneProjectsTo>
                        <!-- 此种方式一直没有测试成功,也没有找出是什么问题
                        <pomIncludes>
                            <pomInclude>C:Maven-Build-Automationprojectsapp-web-uipom.xml</pomInclude>
                            <pomInclude>C:Maven-Build-Automationprojectsapp-desktop-uipom.xml</pomInclude>
                        </pomIncludes>
                         -->
                    </configuration>
                    <executions>
                        <execution>
                            <id>id-integration-test</id>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    注意:<projectsDirectory>节点指定的是app-web-ui和app-desktop-ui项目的目录C:Maven-Build-Automationprojects。

    由于 maven-invoker-plugin 插件绑定的 Maven 生命周期阶段为 integration-test 以上,所以在命令行上输入 integration-test 阶段及其以上的都可以触发。

    详细的Maven生命周期参考:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

    此时在C:Maven-Build-Automationus-core-api执行命令:

    mvn integration-test
    C:Maven-Build-Automationus-core-api>mvn integration-test
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building 总线核心 Api 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bus-core-api ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ bus-core-api ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to C:Maven-Build-Automationus-core-api	argetclasses
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ bus-core-api ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ bus-core-api ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ bus-core-api ---
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ bus-core-api ---
    [INFO] Building jar: C:Maven-Build-Automationus-core-api	argetus-core-api-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] --- maven-invoker-plugin:2.0.0:run (id-integration-test) @ bus-core-api ---
    [INFO] Building: app-desktop-uipom.xml
    [INFO] ..SUCCESS (3.5 s)
    [INFO] Building: app-web-uipom.xml
    [INFO] ..SUCCESS (3.5 s)
    [INFO] -------------------------------------------------
    [INFO] Build Summary:
    [INFO]   Passed: 2, Failed: 0, Errors: 0, Skipped: 0
    [INFO] -------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 10.647 s
    [INFO] Finished at: 2018-03-28T13:49:08+08:00
    [INFO] Final Memory: 16M/161M
    [INFO] ------------------------------------------------------------------------
    
    C:Maven-Build-Automationus-core-api>

    测试成功输出所有项目的jar包。

  • 相关阅读:
    变色龙
    生在北极圈内的“瑞典之声”Sofia Jannok
    “清一色”数列1,11,111,1111,...
    1100 10100 PAGE ERROR
    The Lazarus Project
    数字“黑洞”495
    don't you forget the EXIF~!
    行和列
    小谈奇数平方与偶数平方
    “码农”的得力工具math
  • 原文地址:https://www.cnblogs.com/dyh004/p/11579801.html
Copyright © 2011-2022 走看看