Maven 简明教程
Maven - Build Automation
构建自动化定义了依赖项目构建过程在项目构建成功完成后启动的方案,以确保依赖项目稳定。
Build Automation defines the scenario where dependent project(s) build process gets started once the project build is successfully completed, in order to ensure that dependent project(s) is/are stable.
Example
考虑一个团队正在开发一个项目 bus-core-api ,另两个项目 app-web-ui 和 app-desktop-ui 依赖于该项目。
Consider a team is developing a project bus-core-api on which two other projects app-web-ui and app-desktop-ui are dependent.
app-web-ui 项目正在使用 bus-core-api 项目的 1.0-SNAPSHOT。
app-web-ui project is using 1.0-SNAPSHOT of bus-core-api project.
<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。
app-desktop-ui project is using 1.0-SNAPSHOT of bus-core-api project.
<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 项目 -
bus-core-api project −
<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-ui 和 app-desktop-ui 项目的团队要求只要 bus-core-api 项目发生更改,就应启动他们的构建过程。
Now, teams of app-web-ui and app-desktop-ui projects require that their build process should kick off whenever bus-core-api project changes.
使用快照可确保使用最新的 bus-core-api 项目,但为了满足上述要求,我们需要做一些额外的事情。
Using snapshot, ensures that the latest bus-core-api project should be used but to meet the above requirement we need to do something extra.
我们可以采用以下两种方式进行:
We can proceed with the following two ways −
-
Add a post-build goal in bus-core-api pom to kick-off app-web-ui and app-desktop-ui builds.
-
Use a Continuous Integration (CI) Server like Hudson to manage build automation automatically.
Using Maven
更新 bus-core-api 项目 pom.xml。
Update bus-core-api project 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 命令。
Let’s open the command console, go to the C:\ > MVN > bus-core-api directory and execute the following mvn command.
>mvn clean package -U
Maven 将开始构建 bus-core-api 项目。
Maven will start building the project 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 项目。
Once bus-core-api build is successful, Maven will start building the app-web-ui project.
[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 项目。
Once app-web-ui build is successful, Maven will start building the app-desktop-ui project.
[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] -------------------------------------------------------------------
Using Continuous Integration Service with Maven
开发人员更倾向于使用 CI 服务器。每当添加一个新项目(例如 app-mobile-ui)时,都不需要更新 bus-core-api 项目,因为该项目是 bus-core-api 项目的依赖项目。Hudson 是一种用 Java 编写的持续集成工具,它包含在一个 servlet 容器中,例如 Apache tomcat 和 glassfish 应用服务器。Hudson 使用 Maven 依赖管理自动管理构建自动化。以下快照将定义 Hudson 工具的角色。
Using a CI Server is more preferable to developers. It is not required to update the bus-core-api project, every time a new project (for example, app-mobile-ui) is added, as dependent project on bus-core-api project. Hudsion is a continuous integration tool written in java, which in a servlet container, such as, Apache tomcat and glassfish application server. Hudson automatically manages build automation using Maven dependency management. The following snapshot will define the role of Hudson tool.
Hudson 将每个项目构建视为作业。一旦将项目代码检入到 SVN(或映射到 Hudson 的任何源管理工具)中,Hudson 就会开始其构建作业,一旦此作业完成,它会自动开始其他依赖作业(其他依赖项目)。
Hudson considers each project build as job. Once a project code is checked-in to SVN (or any Source Management Tool mapped to Hudson), Hudson starts its build job and once this job gets completed, it start other dependent jobs (other dependent projects) automatically.
在上面的示例中,当 bus-core-ui 源代码在 SVN 中更新时,Hudson 就会开始其构建。构建成功后,Hudson 会自动查找依赖项目,并开始构建 app-web-ui 和 app-desktop-ui 项目。
In the above example, when bus-core-ui source code is updated in SVN, Hudson starts its build. Once build is successful, Hudson looks for dependent projects automatically, and starts building app-web-ui and app-desktop-ui projects.