Build and package a Java application using Jenkins, Git and Maven

jenkins

In this document we will be going through an outline of compiling and packaging a Java application using Jenkins. Here Jenkins downloads all files (few java codes) from GitHub repository and uses Maven to compile and pack the code in to a “.jar” file.

Prerequisite:

  • CentOS host with 10GB free space and 2GB RAM
  • Install Git in your local host and test if you can download contents of a public repository
  • Install Maven (apache-maven-3.5.3), and see if you can build a sample Java program
  • Install Jenkins (ver. 2.111). Ensure the plugins to integrate with Git and Maven is also installed along with other default plugins. This includes the following plugins which can be installed by going to the Jenkins dashboard, then clicking on “Manage Jenkins” link on left, and selecting “Manage Plugin” link:
    • GitHub plugin
    • Git plugin
    • Git client plugin
    • Maven Integration
  • Create a folder named “workspace” in the host via ssh terminal, inside the folder “/var/lib/jenkins/“, and create a file names “pom.xml” with the following content:
  • <project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.shijuvarghese</groupId>
    <artifactId>helloWorld</artifactId>
    <version>1.0</version>
    </project>
  • run the command “chown -R jenkins.jenkins /var/lib/jenkins/workspace

Procedure:

  • Access the Jenkins dashboard by accessing the URL “http://<host IP>:8080
  • First let us setup settings to connect to Maven.
    • Go to “Manage Jenkins“, then select “Global Configuration Tool“. Under the heading Maven, clink the “Add Maven” beside “Maven installations“.
      Un-check “Install automatically
    • For the Name text-field, enter “Maven 3.5.3” if this is the version of Maven installed as per the pre-requisite section in this tutorial.
    • Find the “MAVEN_HOME” variable configured in the Maven installation in the host by accessing the host via ssh and executing the command “echo $MAVEN_HOME“. Copy and enter the value in the “MAVEN_HOME” text-field in Jenkins.
    • Click on “Apply” and then “Save“.
  • Go back to Jenkins dashboard.
  • Click on New Job or New Item link.
  • Enter the Item Name. For Example “hworld_packaging
  • Select “Maven Project” and click OK.
  • Add a Description. For example “Sample Java application that will be build and packaged“.
  • If needed, select “Discard old builds” checkbox, inter the value “2” in the text-field “Max no of builds to keep”.
  • Select “Git” under the section “Source Code Management“, and add the “Repository URL”. If the ULR is not valid, a message will be displayed. If is advisable to use a publicly available GitHub repository. Steps to find this URL is mentioned in one of my previous documents about Git.
  • Under “Build Triggers” select the checkbox “Poll SCM” and enter the value “H * * * *” in the text-field labeled Schedule.
  • Under the “Build” section, in the “Root POM” text-field enter the value “/var/lib/jenkins/workspace/pom.xml
  • In the “Goals and options” text-field enter the value “clean package
  • Click on Apply and Save.

You will be in the specific project’s page now. Else go to the Dashboard, where you will find the link to go to the newly created project.

Running the project:

  • Go to the newly created project’s page and click on the link “Build now” link on the left.
  • In the “Build History” section, click on the newly created Build number.
  • In the new page, select the “Console Output” link, where you will see the results. Make sure the last section of the output says “Finished: SUCCESS“.
  • You may confirm is the accessing the server and looking for “/var/lib/jenkins/workspace/hworld_packaging/target/helloWorld-1.0.jar” as mentioned in the “Console Output” in the Jenkins page.