Maven實現多個項目關聯自動化構建(maven-invoker-plugin插件的使用)


以下內容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_automation.html

注意:由於時間問題,原文的方法已經無法使用,在此我更新了最新的方法去實現。同時也是官方原版的方法:https://www.tutorialspoint.com/maven/maven_build_automation.htm

一、場景

設想一個團隊正在開發一個項目bus-core-api, 並且有其他兩個項目app-web-uiapp-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-uiapp-desktop-ui項目的團隊要求的是不管bus-core-api項目何時變化,他們的構建過程都應當可以啟動。

使用快照確保了最新的bus-core-api項目會被使用,但要達到上面的要求,我們還需要做一些額外的工作。

提示:其實這個場景有一點矛盾,但是為了演示效果,可以這樣理解,即當bus-core-api項目構建時,自動構建app-web-uiapp-desktop-ui項目。

二、構建方式選擇

  • bus-core-api項目的pom.xml文件中添加一個maven-invoker-plugin插件操作來啟動app-web-uiapp-desktop-ui項目的構建。
  • 使用持續集成(CI) 服務器,比如Jenkins,來自行管理構建自動化。(省略)
  • 使用腳本實現(Linux/Windows)(省略)

三、使用maven-invoker-plugin插件操作實現詳解

maven-invoker-plugin插件詳細用法參考:http://maven.apache.org/plugins/maven-invoker-plugin/

准備環境:

1、建立目錄C:\MVNC:\MVN\projects

2、在C:\MVN下創建bus-core-api項目,在C:\MVN\projects下創建app-web-uiapp-desktop-ui項目。

目錄結構如下:

├─bus-core-api
│  ├─src
│  │  ├─main
│  │  │  └─java
│  │  │      └─com
│  │  │          └─jsoft
│  │  │              └─test
│  │  └─test
│  │      └─java
│  │          └─com
│  │              └─jsoft
│  │                  └─test
│  └─target
│      ├─classes
│      │  └─com
│      │      └─jsoft
│      │          └─test
│      ├─invoker-reports
│      ├─maven-archiver
│      ├─surefire-reports
│      └─test-classes
│          └─com
│              └─jsoft
│                  └─test
└─projects
    ├─app-desktop-ui
    │  ├─src
    │  │  ├─main
    │  │  │  └─java
    │  │  │      └─com
    │  │  │          └─jsoft
    │  │  │              └─test
    │  │  └─test
    │  │      └─java
    │  │          └─com
    │  │              └─jsoft
    │  │                  └─test
    │  └─target
    │      ├─classes
    │      │  └─com
    │      │      └─jsoft
    │      │          └─test
    │      ├─maven-archiver
    │      ├─surefire
    │      ├─surefire-reports
    │      └─test-classes
    │          └─com
    │              └─jsoft
    │                  └─test
    └─app-web-ui
        ├─src
        │  ├─main
        │  │  └─java
        │  │      └─com
        │  │          └─jsoft
        │  │              └─test
        │  └─test
        │      └─java
        │          └─com
        │              └─jsoft
        │                  └─test
        └─target
            ├─classes
            │  └─com
            │      └─jsoft
            │          └─test
            ├─maven-archiver
            ├─surefire
            ├─surefire-reports
            └─test-classes
                └─com
                    └─jsoft
                        └─test

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>app-web-ui</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>app-web-ui</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>     <groupId>com.jsoft.test</groupId>     <artifactId>bus-core-api</artifactId>     <version>1.0-SNAPSHOT</version>     <scope>system</scope>     <systemPath>C:\MVN\bus-core-api\target\bus-core-api-1.0-SNAPSHOT.jar</systemPath> </dependency>
  </dependencies>
</project>        

提示:為了測試,設置bus-core-api項目依賴為本地依賴。其中C:\MVN\bus-core-api\target\bus-core-api-1.0-SNAPSHOT.jarbus-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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>app-desktop-ui</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>app-desktop-ui</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>     <groupId>com.jsoft.test</groupId>     <artifactId>bus-core-api</artifactId>     <version>1.0-SNAPSHOT</version>     <scope>system</scope>     <systemPath>C:\MVN\bus-core-api\target\bus-core-api-1.0-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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsoft.test</groupId>
  <artifactId>bus-core-api</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>bus-core-api</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
       <plugin>       <artifactId>maven-invoker-plugin</artifactId>       <version>2.0.0</version>       <configuration>       <debug>true</debug>       <projectsDirectory>C:\MVN\projects</projectsDirectory>       </configuration>       <executions> <execution> <id>id-integration-test</id> <goals> <goal>run</goal> </goals> </execution>       </executions>     </plugin>
    </plugins>
   </build>
</project>

注意:<projectsDirectory>節點指定的是app-web-uiapp-desktop-ui項目的目錄C:\MVN\projects

由於maven-invoker-plugin插件綁定的Maven生命周期階段為integration-test以上,所以在命令行上輸入integration-test階段及其以上的都可以觸發。

詳細的Maven生命周期參考:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

此時在C:\MVN\bus-core-api執行命令:

mvn integration-test

測試成功輸出所有項目的jar包。

 

測試代碼:https://github.com/easonjim/5_java_example/tree/master/maventest/test5

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM