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.
Table of Contents
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
#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.
once the plugins are downloaded successfully. So now click on Restart Jenkins checkbox.
Please wait while Jenkins is restarting
#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
Click on JDK->> Add JDK, Give a JDK name and provide your JAVA_HOME path where the JDK is present.
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.
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.
Enter Job name and select “Pipeline”, Click OK.
#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
Now we will set the Build Triggers, Select Build whenever a SNAPSHOT and GitHub hook trigger for GITScm polling as triggers.
Now in Pipeline section click on drop down and select Pipeline script from SCM.
In SCM select Git and below it provide your Git repository URL where your project is located and Add your GitHub credentials.
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
Now comes the most important part of your Project / Job configuration, Provide accurate path of your Jenkinsfile.
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.
check the console output to check Jenkins pipeline logs.
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: