Building Maven Project with Jenkins Pipeline (Jenkins docker image)

Syed Kamran Hussain
4 min readOct 2, 2018

--

Pre-Requisite: Environment setup

  1. Open up a command prompt window.
  2. Run the jenkinsci/blueocean image as a container in Docker using the following docker run command (bearing in mind that this command automatically downloads the image if this hasn’t been done): docker run — rm -u root -p 8080:8080 -v jenkins-data:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v “%HOMEDRIVE%%HOMEPATH%”:/home jenkinsci/blueocean
  3. After the 2 sets of asterisks appear in the terminal/command prompt window, browse to http://localhost:8080 and wait until the Unlock Jenkins page appears.

From your terminal/command prompt window again, copy the automatically-generated alphanumeric password (between the 2 sets of asterisks).

Next create the first admin user.

Next install suggested plugins.

Now that our environment is ready, let’s begin the actual tutorial

Fork and clone the sample repository on GitHub

  1. Ensure you are signed in to your GitHub account. If you don’t yet have a GitHub account, sign up for a free one on the GitHub website.
  2. Fork the simple-java-maven-app on GitHub into your local GitHub account. If you need help with this process, refer to the Fork A Repo documentation on the GitHub website for more information.
  3. Clone your forked simple-java-maven-app repository (on GitHub) locally to your machine

Create your Pipeline project in Jenkins

  1. Go back to Jenkins, log in again if necessary and click create new jobs under Welcome to Jenkins!
    Note: If you don’t see this, click New Item at the top left.
  2. In the Enter an item name field, specify the name for your new Pipeline project (e.g. simple-java-maven-app).
  3. Scroll down and click Pipeline, then click OK at the end of the page.
  4. ( Optional ) On the next page, specify a brief description for your Pipeline in the Description field (e.g. An entry-level Pipeline demonstrating how to use Jenkins to build a simple Java application with Maven.)
  5. Click the Pipeline tab at the top of the page to scroll down to the Pipeline section.
  6. From the Definition field, choose the Pipeline script from SCM option. This option instructs Jenkins to obtain your Pipeline from Source Control Management (SCM), which will be your locally cloned Git repository.
  7. From the SCM field, choose Git.
  8. In the Repository URL field, specify the directory path of your locally cloned repository, which is from your user account/home directory on your host machine, mapped to the /home directory of the Jenkins container - i.e. - /home/Documents/GitHub/simple-java-maven-app
  9. Click Save to save your new Pipeline project. You’re now ready to begin creating your Jenkinsfile, which you’ll be checking into your locally cloned Git repository.

Create your initial Pipeline as a Jenkinsfile

You’re now ready to create your Pipeline that will automate building your Java application with Maven in Jenkins. Your Pipeline will be created as a Jenkinsfile, which will be committed to your locally cloned Git repository (simple-java-maven-app).

This is the foundation of “Pipeline-as-Code”, which treats the continuous delivery pipeline as a part of the application to be versioned and reviewed like any other code.

Task 1:

Create an initial Pipeline to download a Maven Docker image and run it as a Docker container (which will build your simple Java application). Also add a “Build” stage to the Pipeline that begins orchestrating this whole process.

Using any text editor , create and save new text file with the name Jenkinsfile at the root of your local simple-java-maven-app Git repository.

Copy the following Declarative Pipeline code and paste it into your empty Jenkinsfile:

pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
}
stage('Deliver') {
steps {
sh './jenkins/scripts/deliver.sh'
}
}
}
}

Save your edited Jenkinsfile and commit it to your local simple-java-maven-app Git repository. E.g. Within the simple-java-maven-app directory, run the commands:
git stage .
then
git commit -m "Add 'Deliver' stage"

Go back to Jenkins again, log in again if necessary and ensure you’ve accessed Jenkins’s Blue Ocean interface.

Click Run at the top left, then quickly click the OPEN link which appears briefly at the lower-right to see Jenkins running your amended Pipeline project. If you weren’t able to click the OPEN link, click the top row on the Blue Ocean interface to access this feature.

Here’s what the output of the pipeline looks like.

Click the X at the top-right to return to the main Blue Ocean interface, which lists your previous Pipeline runs in reverse chronological order.

Here we have finished building a Maven based Java build pipeline with Jenkinsfile.

--

--