Build Java Project using Maven in Jenkins Pipeline

In this article we are going to cover Install GitHub Integration and Maven Plugins in Jenkins, Build Java Project using Maven in Jenkins Pipeline, Global Tool configuration in Jenkins.

Create Pipeline Job in Jenkins, Enter Project details in Jenkins Pipeline, Jenkinsfile to Build Java Project using Maven in Jenkins Pipeline.

Prerequisites

  • Preinstalled Jenkins with Admin Access
  • Installed JDK
  • Installed Maven
  • GitHub Repository

What is Jenkins Pipeline ?

Jenkins Pipeline is a combination of Plugins which automates number of tasks and makes the CI/CD pipeline efficient, high in quality and reliable.

What is Jenkinsfile ?

Jenkinsfile is nothing but a simple text file which is used to write the Jenkins Pipeline and to automate the Continuous Integration process.

Jenkinsfile works as a “Pipeline as a Code”.

Jenkinsfile is written in couple of way

  • Declarative pipeline syntax
  • Scripted pipeline syntax

#1: Login to Jenkins

First thing, we will login to our Jenkins account

Build Java Project using Maven in Jenkins Pipeline 1

#2. Install GitHub Integration and Maven Plugins in Jenkins

To build Java project with maven we need to install some plugins like,

  • GitHub Integration Plugin
  • Maven Integration Plugin

To install plugins follow below steps

Navigate to Dashboard->> Manage Jenkins ->> Manage Plugin

Now search for “GitHub Integration Plugin” and click on Download now and install after restart.

Build Java Project using Maven in Jenkins Pipeline 2

once the plugins are downloaded successfully. So now click on Restart Jenkins checkbox.

Build Java Project using Maven in Jenkins Pipeline 3

Please wait while Jenkins is restarting

Build Java Project using Maven in Jenkins Pipeline 4

#3. Global Tool configuration in Jenkins

Now we will configure System by adding JDK and Maven installation in Jenkins.

Navigate to Dashboard->> Manage Jenkins->> Global tool Configuration

Build Java Project using Maven in Jenkins Pipeline 5

Click on JDK->> Add JDK, Give a JDK name and provide your JAVA_HOME path where the JDK is present.

Build Java Project using Maven in Jenkins Pipeline 6

Now below click on Maven->> Add Maven, We can provide our Installed Maven path or we can also use Jenkins default one.In this case we are using Jenkins default Maven version.

Build Java Project using Maven in Jenkins Pipeline 7

Click on apply and save.

#4. Create Pipeline Job in Jenkins

Now we will create a new Pipeline Job in Jenkins, So, click on “New Item” on the Jenkins Dashboard as shown below.

Build Java Project using Maven in Jenkins Pipeline 8

Enter Job name and select “Pipeline”, Click OK.

Build Java Project using Maven in Jenkins Pipeline 9

#5. Enter Project details in Jenkins Pipeline

Provide some description for the newly created job in General Section, Click on GitHub project and provide the GitHub URL.

you can use Sample Java Project on GitHub with jenkinsfile

Build Java Project using Maven in Jenkins Pipeline 10

Now we will set the Build Triggers, Select Build whenever a SNAPSHOT and GitHub hook trigger for GITScm polling as triggers.

Build Java Project using Maven in Jenkins Pipeline 11

Now in Pipeline section click on drop down and select Pipeline script from SCM.

Build Java Project using Maven in Jenkins Pipeline 12

In SCM select Git and below it provide your Git repository URL where your project is located and Add your GitHub credentials.

Build Java Project using Maven in Jenkins Pipeline 13

By default Jenkins provide branch as Master but you can change it to dev, main, or to whatever your branch is.(if you don’t know you can check the branch by going to your GitHub repository OR by running git branch command on your shell).

Our branch is main branch so will change master to main

Build Java Project using Maven in Jenkins Pipeline 14

Now comes the most important part of your Project / Job configuration, Provide accurate path of your Jenkinsfile.

Build Java Project using Maven in Jenkins Pipeline 15

Click on Apply and Save.

#6. Jenkinsfile to Build Java Project using Maven in Jenkins Pipeline

Now we see how to write a Declarative Pipeline script to build java project using maven.

Why Declarative Pipeline ?

Declarative Pipeline is a latest method mostly used in now a day to write Pipeline rather that that typical way (Scripted) because Declarative provides new feature which can even be tested in Git for source control)

Below is Jenkinsfile to Build Java Project using Maven in Jenkins and genrate junit test report.

pipeline {
    agent any
    tools {
        maven "MAVEN"
        jdk "JDK"
    }
    stages {
        stage('Initialize'){
            steps{
                echo "PATH = ${M2_HOME}/bin:${PATH}"
                echo "M2_HOME = /opt/maven"
            }
        }
        stage('Build') {
            steps {
                dir("/var/lib/jenkins/workspace/demopipelinetask/my-app") {
                sh 'mvn -B -DskipTests clean package'
                }
            }
        }
     }
    post {
       always {
          junit(
        allowEmptyResults: true,
        testResults: '*/test-reports/.xml'
      )
      }
   } 
}

Note- Replace and add your workspace path in dir.

#7. Build Java Project using Maven in Jenkins Pipeline

Now open your Jenkins Dashboard and click on your job, Now click on Build Now

you can see Jenkins Pipeline Build stages.

Build Java Project using Maven in Jenkins Pipeline 16

check the console output to check Jenkins pipeline logs.

Build Java Project using Maven in Jenkins Pipeline 17

We have covered How to Build Java Project using Maven in Jenkins Pipeline.

Conclusion:

In this article we studied How to Build Java Project Using Maven In Jenkins Pipeline.First we learned how to add a Build Stage and print message.Next we done with adding the tools like JDK and Maven.Then at last we explored how to Run a Maven Build with JUnit.

Related Articles:

Akash Padwal

I am Akash Padwal working as a DevOps Engineer Intern in Product Company. Likes to explore new DevOps tools and share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap