Signup/Sign In

Maven Build Automation

Build automation is the process in which a project gets initiated with the build process whenever a change is made in the workspace and also to ensure that project is stable along with its dependent projects (if the project is used by any other projects).

This is very much essential in the software development life cycle as it is difficult to manage with the failed builds during the development phase. Hence, a process is required in order to ensure the health status of the build all the time and keep an eye on the same.

Consider the project JavaSamples in this scenario. Here, lot of programs varying across different oops concept will be written and tested. But, it is always difficult to test every class independently for the compilation issue since they are modularized under different set of packages.

In order to achieve this, maven provides a feature to automate the same. Consider, developers want to check the build stability after each check in done to the project. Also, consider that this JavaSamples project is a dependent project of another project called CoreJavaTutorials. Hence, it is mandatory to maintain a stable build of JavaSamples.

Below mentioned are the pom.xml files for two dependent projects.

CoreJavaTutorials 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>CoreJavaTutorials</groupId>
   <artifactId>CoreJavaTutorials </artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <dependencies>
      <dependency>
      <groupId>JavaSamples</groupId>
         <artifactId>JavaSamples</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
   </dependencies>
</project>

JavaSamples 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>JavaSamples</groupId>
   <artifactId>JavaSamples</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
</project>

Now consider that there are some changes made on the JavaSamples project. Developer need to update the pom.xml of the JavaSamples project by adding a post build goal on the same.

post build goal is nothing but, a goal which defines a specific set of tasks once the build is successful. For e.g. generating javadocs is a post build goal. We will study about generating Javadocs in coming tutorials.

<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>JavaSamples</groupId>
   <artifactId>JavaSamples</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <build>
      <plugins>
         <plugin>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.6</version>
            <executions>
               <execution>
                  <id>build</id>
                  <goals>
                     <goal>run</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>
</project>

Build Automation using Jenkins Server

Jenkins Server(click for complete Jenkins tutoriali>) is a continuous integration tool. Using Jenkins build can be automated, tested for the health status of the builds and also multiple builds can be managed. It is a handy tool which is easily available to download and very easy to set up.

Setting up Jenkins Server

  • Download the latest Jenkins.war from following link : http://jenkins-ci.org/

    Installing Jenkins Server

  • Deploy the Jenkins.war file in the local web server of our machine. E.g. Tomcat
  • Start the server and open the browser and hit the URL http://localhost:8080/jenkins

    Setting up Jenkins Server and Automating Maven Build


Creating a new Job and Deploying the Build

Click on the Create new jobs link, shown in picture above, to configure a project for build automation. Enter the details of the project as shown below and click OK. You must enter the Name and select the radio button for Maven Project.

Setting up Jenkins Server and Automating Maven Build


In the next page, fill all the required details like - Description, Path of the project's pom.xml and other details and click save. Please check the below picture for help. Fill all the information and click on SAVE

Setting up Jenkins Server and Automating Maven Build

Setting up Jenkins Server and Automating Maven Build

Setting up Jenkins Server and Automating Maven Build


A Project/Job is now created in Jenkins. As shown below :

Setting up Jenkins Server and Automating Maven Build


Click on the Build now option as shown above. This will read the pom.xml and pull out the latest code and executes a build process and generates the jar/war file accordingly.

Setting up Jenkins Server and Automating Maven Build


Click on the build number link and it opens up the build details. Click on the Console Output to see the logs of the build.

Build Automation in Maven using Jenkins Server


Check the logs and see your project building successfully.

Build Automation in Maven using Jenkins Server

Build Automation in Maven using Jenkins Server