How to create Jenkins Pipeline for Java Project

Introduction

Jenkins Pipeline (or simply “Pipeline”) is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

A continuous delivery pipeline is an automated expression of your process for getting software from version control right through to your users and customers.

Jenkins Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code”. The definition of a Jenkins Pipeline is typically written into a text file (called a Jenkinsfile) which in turn is checked into a project’s source control repository.

Why do we need Jenkins Pipeline?

Let’s say you are developing an application which you want to build, test and deploy. Therefore you need three jobs for building, testing and deploying, respectively. So after creating three jobs and chaining them into sequence, the build plugin will run them as a pipeline job.

The above process is quite simple to implement for the small application but when you have many jobs with several processes, such as, build, unit test, integration test, pre-deploy, deploy, post-deploy etc., will have huge maintenance cost and complexities. To overcome such issues Jenkins Pipeline Project comes to rescue.

The key feature of this pipeline is to define the entire deployment flow through code in a file called Jenkinsfile. A Jenkinsfile is a text file that stores the entire workflow as code and it can be checked into a repository system.

Prerequisites

Setup Jenkins, Java, Gradle

Create Jenkins Pipeline

To create jenkins pipeline, login to Jenkins and click on New Item.

jenkins pipeline

Next enter a name for your pipeline and click on Pipeline and then Ok.

jenkins pipeline

Next scroll down and choose Pipeline script or Pipeline script from SCM.

jenkins pipeline

I am going to use declarative pipeline, so I select Pipeline script from SCM and choose your SCM. In my case I’m going to use Git. Enter your repository URL.

jenkins pipeline

For my example, the URL is https://github.com/roytuts/jenkins-pipeline.

The Jenkinsfile is automatically detected by Jenkins if available in the root of your project directory.

Since this is a declarative pipeline, I’m writing the code in a file named Jenkinsfile. While executing the pipeline example project, this file will be accessed from my git repository. The following is a simple demonstration of building a pipeline to run multiple stages, each performing a specific task.

The code is written within the pipeline {} block.

We want to run the pipeline on any available executor, so we have defined agent as any.

Next I have defined several stages to perform certain tasks.

Stage One executes simple echo command. So the message Hi, this is Soumitra from roytuts will be printed on the Jenkins console.

Stage Two executes an input directive that allows a user to prompt a user input. If user approves then only it will execute further. If user aborts then it will terminate.

Stage build builds the application’s source code. We are using Windows environment, so we have used command bat './gradlew build', if you are using Unix environment then you need to use sh './gradlew build'.

Stage Test executes the Junit test cases.

Stage Check verifies if everything was fine during build/test process of your application.

Stage Five just echos the message Finished.

pipeline {
	agent any
	stages {
		stage('One') {
			steps {
				echo 'Hi, this is Soumitra from roytuts'
			}
		}
		
		stage('Two') {
			steps {
				input('Do you want to proceed?')
			}
		}
		
		stage('Build') {
            steps {
                bat './gradlew build'
            }
        }
        
        stage('Test') {
            steps {
                bat './gradlew test'
            }
        }
        
        stage('Check') {
            steps {
                bat './gradlew check'
            }
        }      
		
		stage('Five') {
			steps {
				echo 'Finished'
			}
		}		
	}
}

The Jenkins console view where user needs to approve or abort the build process.

jenkins pipeline

Clicking on Proceed will finish the build job with success if there is no error.

jenkins pipeline

On stage view you will find your builds as shown in the below image.

You can find the source code used for this Jenkins pipeline here.

That’s all. I hope you got an idea on how to create Jenkins pipeline for Java project.

There are many features of Jenkins pipeline and you can easily explore at the official documentation.

Thanks for reading.

Related posts

Leave a Comment