Maven Automated Build
Automated build defines a scenario like this: after a project is successfully built, its related dependent projects start building, which ensures the stability of its dependent projects.
For example, a team is developing a project bus-core-api, and two other projects, app-web-ui and app-desktop-ui, depend on this project.
The app-web-ui project uses the 1.0 snapshot of the 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
</
projectThe app-desktop-ui project uses the 1.0 snapshot of the 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
<
dependencies
<
dependency
<
groupId
bus-core-api
</
groupId
<
artifactId
bus-core-api
</
artifactId
<
version
1.0-SNAPSHOT
</
version
</
dependency
</
dependencies
</
projectThe 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
</
projectNow the teams of the app-web-ui and app-desktop-ui projects require that, no matter when the bus-core-api project changes, their build process should be able to start.
Using a snapshot ensures that the latest bus-core-api project is used, but to meet the above requirement, we need to do some additional work.
Two approaches can be used:
- Add a post-build goal operation in the pom file of the bus-core-api project to start the builds of the app-web-ui and app-desktop-ui projects.
- Use a continuous integration (CI) server, such as Hudson, to manage build automation by itself.
Using Maven
Modify the pom.xml file of the 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
<
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
</
projectOpen the command console, switch to the C:\ > MVN > bus-core-api directory, and then execute the following command.
C:\MVN\bus-core-api>mvn clean package -U
After executing the command, 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] ------------------------------------------------------------------
After bus-core-api is built successfully, 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] ------------------------------------------------------------------
After app-web-ui is built successfully, 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 a Continuous Integration Server (CI)
If a CI server is used, then whenever we add a new project, for example app-mobile-ui in the example, as a dependency of the bus-core-api project, developers do not need to update the pom of the bus-core-api project. Hudson will rely on Maven's dependency management features to achieve automatic creation of the project.

Hudson treats each project build as a task. After a project's code is committed to SVN (or any code management tool mapped to Hudson), Hudson will start the project's build task, and once this build task is complete, Hudson will automatically start the build tasks of other dependents (the builds of other dependent projects).
In the above example, when the source code of bus-core-ui is updated in SVN, Hudson starts the project build. Once the build succeeds, Hudson automatically finds the dependent projects and then starts building the app-web-ui and app-desktop-ui projects.