Jenkinsfile for a Maven Build

Jenkinsfile is a great concept, however, I have found documentation is lacking.  To that end, a simple Jenkinsfile for building a Maven project should not be very difficult. With that in mind let me walk you through the steps on what you need to create for this simple process.

Completed Jenkinsfile:

Let’s break this down.

“pipeline” is the wrapper for performing multiple steps in your file.  Not much to configure here, just make sure you wrap the content of your file in this. At this stage your source code will be loaded from your source control management.

“agent” defines the node that should be used for the stages in the pipeline.  In this case I am using a Dockerfile that is part of my project. This could as easily be label indicating a particular group of slave nodes in Genkins, or a Docker Image to pull from Docker Hub.

“stages” contains the steps that make up the pipeline.

“stage” is a single step in the group of stages.  Each stage may have multiple steps.  As you can see here our step is “sh ‘mvn clean install'”, the sh indicates it is a shell command that is executed and the ‘mvn clean install’ is the actual command to be executed. In this case we are executing the maven command to build my project.

On this stage your see a new container “parallel” this indicates the steps it contains can be executed in parallel.

Here you can see we are performing the “Junit” and “Archive” steps in parallel.

Here you can see we have issued another command “junit ‘target/surefire-reports/*.xml'” “junit” identifies this to collect the JUnit report files that were generated.  The “‘target/surefire-reports/*.xml'” states where those files can be found.

Here is the “Archive” step.  Again we have a new command “archiveArtifacts()”  this takes multiple parameters.  The “Artifacts” the set fo files to collect, “onlyIfSuccessful” a boolean indicating to collect the artifacts only if the build is successful, and last fingerprint indicating if the artifact should be fingerprinted.

There are many other steps and configurations that you could have as well, however, this is just  simple Jenkinsfile for a simple maven project.

One thought on “Jenkinsfile for a Maven Build

Leave a Reply

Your email address will not be published. Required fields are marked *