How to Build Java Project using Maven in Jenkins

In this article we are going to cover How to Build Java Project using Maven in Jenkins, Install GitHub and Maven Integration Plugins in Jenkins, Global Tool configuration in Jenkins, Create Maven Project in Jenkins, Enter Project Details in Jenkins and Build maven project in Jenkins.

Prerequisites

  • Installed Jenkins with login credentials

How to Build Java Project using Maven in Jenkins


#1: Login to Jenkins

First thing, we will login to our Jenkins account with username and password.

How to Build Java Project using Maven in Jenkins 1

#2: Install GitHub and Maven Integration 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.

How to Build Java Project using Maven in Jenkins 2

We will do the same for “Maven Integration Plugin” and click on Download now and install after restart

How to Build Java Project using Maven in Jenkins 3

As we can see plugins are downloaded successfully. So now click on Restart Jenkins checkbox.

How to Build Java Project using Maven in Jenkins 4

Please wait while Jenkins is restarting

How to Build Java Project using Maven in Jenkins 5

#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

How to Build Java Project using Maven in Jenkins 6

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

How to Build Java Project using Maven in Jenkins 7

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.

How to Build Java Project using Maven in Jenkins 8

Click on apply and save.

#4: Create Maven Project in Jenkins

Now we will create a new Job (maven project) in Jenkins, So, click on “New Item” on the Jenkins Dashboard.

How to Build Java Project using Maven in Jenkins 9

Enter Job name and select “Maven Project” and Click OK.

How to Build Java Project using Maven in Jenkins 10

#5: Enter Project Details in Jenkins (source code management).

Give some description for your Project., Now In Source Code Management provide your Git Repository URL or you can use our GitHub Repository for testing.

How to Build Java Project using Maven in Jenkins 11

As you can see, we got an error but no need to worry, As our repository is private so we will need to “Add credentials”.So click on add button, Now select Global as a scope, Provide your GitHub username and password, Provide your GitHub ID, Enter a description for your credentials, Now click on SAVE button.

How to Build Java Project using Maven in Jenkins 12

#6: Set Branch and Build Triggers in Jenkins

Provide your particular github branch.

How to Build Java Project using Maven in Jenkins 13

Select Build whenever a SNAPSHOT and GitHub hook trigger for GITScm polling as triggers.

How to Build Java Project using Maven in Jenkins 14

Now in Build Environment section we have to select “Delete workspace before build starts”.

#7: Provide POM file for Maven Project in Jenkins

Now the most important part of our build is POM file, So in build section provide path of your projects pom file, Set clean and package as Goals and options.

How to Build Java Project using Maven in Jenkins 15

Set Run regardless of built result, Then click on Save and Apply.

How to Build Java Project using Maven in Jenkins 16

#8: Build maven project and check Console Output

Now go to Jenkins Dashboard and we can see our Maven project has been integrated with Jenkins.

Click on Build now.

How to Build Java Project using Maven in Jenkins 17

Now below we can see the build history and click on the latest build.

How to Build Java Project using Maven in Jenkins 18

Now you can see the latest build with date and time, So click on Console Output.

How to Build Java Project using Maven in Jenkins 19
How to Build Java Project using Maven in Jenkins 20

As we can see our Maven Project is Build successfully on Jenkins, we have covered how to Build Java Project using Maven in Jenkins using a GitHub(private repository)

Conclusion:

In this article we have covered how to Build Java Project using Maven in Jenkins using a GitHub(private repository), We studied how to add plugins (Git and Maven), How to configure JDK and Maven in Jenkins, We learned how add Git repository URL and how to add POM file in Jenkins, how to add Build Triggers.

We learned how to add credentials so that Jenkins can gain access to our private / public repository, build a Java Project using Maven in Jenkins.

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