Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven Build Automation


May 26, 2021 Maven


Table of contents


Maven - Build automation

Build automation is defined as a scenario in which once the project is successfully built, its associated dependency works begin to be built in order to ensure the stability of the dependent project.

Consider that a team is developing a project for the bus core API, known as bus-core-api, which relies on two projects, namely the web page UI (called app-web-ui) and the application desktop UI (called app-desktop-ui).

The app-web-ui project uses the 1.0-SNAPSHOT bus core API project, whose POM files are as follows:

<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 Engineering is also using the 1.0-SNAPSHOT Bus Core Api Project, whose POM files are as follows:

<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>
</project>

The POM file for the bus-core-api project is as follows:

<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>

Now, the teams of app-web-ui and app-desktop-ui engineering need to ensure that their own projects can be built at any time when the bus-core-api project changes.

Using snapshots ensures that the latest bus-core-api projects are available, but we still need to do some extra work to meet these requirements.

We have 2 ways:

  • Add a compilation target to the pom file of bus-core-api to alert the app-web-ui project and the app-desktop-ui project to start creation.
  • Automate creation using a continuous integration (CI) server, such as Hudson.

Use Maven

Update the pom file for the bus-core.xml 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>
</project>

Open the command terminal and go to C: In the directory of MVN and bus-core-api, then execute the following mvn command.

C:\MVN\bus-core-api>mvn clean package -U

Maven will begin building the bus-core-api project with the following output logs:

[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] ------------------------------------------------------------------

Once the bus-core-api build is successful, Maven will automatically start building the app-web-ui project, as follows:

[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] ------------------------------------------------------------------

By the time the app-web-ui was created, Maven went on to build the app-desktop-ui project, and the log output was as follows:

[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 ContinuousLy Integrated Server (CI)

Because developers don't need to update the pom files for the bus-core-api project every time there's a new dependent project, it's more appropriate to use a continuously integrated server, for example, to add a new app-mobile-ui project that also relies on the bus-core-ui project. Hudson will automate engineering creation with Maven's dependency management capabilities.

Apache Maven Build Automation

Hunson sees each creation project as a job. Once the engineering code is joined to svn or any other code source management tool mapped to Hudson, Hunson starts a once-in-a-time creation that automatically creates additional related dependent work or dependent projects when the work is complete.

In the example above, Hudson will start creation when the source code for bus-core-api is updated on the SVN. When the creation is complete, Hudson starts automatically looking for the projects it depends on, and then starts the app-web-ui and app-desktop-ui projects.