Maven - 構建自動化



構建自動化定義了一種場景,其中一旦專案構建成功完成,就會啟動依賴專案(s)的構建過程,以確保依賴專案(s)是穩定的。

示例

假設一個團隊正在開發一個名為bus-core-api的專案,另外兩個專案app-web-uiapp-desktop-ui依賴於它。

app-web-ui專案使用bus-core-api專案的1.0-SNAPSHOT版本。

<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>
   <packaging>jar</packaging>
   <dependencies>
      <dependency>
         <groupId>bus-core-api</groupId>
            <artifactId>bus-core-api</artifactId>
            <version>1.0-SNAPSHOT</version>
      </dependency>
   </dependencies>
</project>

app-desktop-ui專案使用bus-core-api專案的1.0-SNAPSHOT版本。

<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>
   <packaging>jar</packaging>
   <name>app_desktop_ui</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>bus_core_api</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專案 -

<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>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>   
</project>

現在,app-web-uiapp-desktop-ui專案的團隊要求,每當bus-core-api專案發生更改時,它們的構建過程都應該啟動。

使用快照可以確保使用最新的bus-core-api專案,但要滿足上述要求,我們需要做一些額外的事情。

我們可以透過以下兩種方式進行操作 -

  • 在bus-core-api的pom中新增一個構建後目標,以啟動app-web-uiapp-desktop-ui的構建。

  • 使用持續整合(CI)伺服器(如Hudson)自動管理構建自動化。

使用Maven

更新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>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <build>
      <plugins>
         <plugin>
         <artifactId>maven-invoker-plugin</artifactId>
         <version>1.6</version>
         <configuration>
            <debug>true</debug>
            <pomIncludes>
               <pomInclude>app-web-ui/pom.xml</pomInclude>
               <pomInclude>app-desktop-ui/pom.xml</pomInclude>
            </pomIncludes>
         </configuration>
         <executions>
            <execution>
               <id>build</id>
               <goals>
                  <goal>run</goal>
               </goals>
            </execution>
         </executions>
         </plugin>
      </plugins>
   <build>
</project>

讓我們開啟命令控制檯,進入C:\ > MVN > bus-core-api目錄,並執行以下mvn命令。

>mvn clean package -U

Maven將開始構建bus-core-api專案。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building bus-core-api
[INFO] task-segment: [clean, package]
[INFO] ------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\bus-core-ui\target\
bus-core-ui-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------

一旦bus-core-api構建成功,Maven將開始構建app-web-ui專案。

[INFO] ------------------------------------------------------------------
[INFO] Building app-web-ui
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\app-web-ui\target\
app-web-ui-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------

一旦app-web-ui構建成功,Maven將開始構建app-desktop-ui專案。

[INFO] ------------------------------------------------------------------
[INFO] Building app-desktop-ui
[INFO] task-segment: [package]
[INFO] ------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\app-desktop-ui\target\
app-desktop-ui-1.0-SNAPSHOT.jar
[INFO] -------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------------------------------

使用持續整合服務與Maven

使用CI伺服器對於開發人員來說更可取。不需要每次新增新的專案(例如,app-mobile-ui)作為bus-core-api專案的依賴專案時都更新bus-core-api專案。Hudsion是一個用Java編寫的持續整合工具,它執行在servlet容器中,例如Apache tomcat和glassfish應用程式伺服器。Hudson使用Maven依賴管理自動管理構建自動化。以下快照將定義Hudson工具的作用。

automated build

Hudson將每個專案的構建視為一個作業。一旦專案的程式碼被簽入SVN(或對映到Hudson的任何原始碼管理工具),Hudson就會啟動其構建作業,一旦該作業完成,它就會自動啟動其他依賴作業(其他依賴專案)。

在上面的示例中,當bus-core-ui原始碼在SVN中更新時,Hudson會啟動其構建。構建成功後,Hudson會自動查詢依賴專案,並開始構建app-web-uiapp-desktop-ui專案。

廣告