Thu. Mar 28th, 2024
jenkinsfile

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:

pipeline {
  agent {
    dockerfile {
      filename 'docker/Dockerfile'
    }
    
  }
  stages {
    stage('compile') {
      steps {
        sh 'mvn clean install'
      }
    }
    stage('archive') {
      steps {
        parallel(
          "Junit": {
            junit 'target/surefire-reports/*.xml'
            
          },
          "Archive": {
            archiveArtifacts(artifacts: 'target/Nadia.jar', onlyIfSuccessful: true, fingerprint: true)
            archiveArtifacts(artifacts: 'target/Nadia*javadoc.jar', fingerprint: true)
            
          }
        )
      }
    }
  }
}

Let’s break this down.

pipeline {
...
}

“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 {
    dockerfile {
      filename 'docker/Dockerfile'
    }
   
  }

“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 {
     ...
  }

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

    stage('compile') {
      steps {
        sh 'mvn clean install'
      }
    }

“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.

    stage('archive') {
      steps {
        parallel(
           ...
        )
      }
    }

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

    stage('archive') {
      steps {
        parallel(
          "Junit": {
            ...
          },
          "Archive": {
            ...
          }
        )
      }
    }

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

          "Junit": {
            junit 'target/surefire-reports/*.xml'
            
          },

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.

          "Archive": {
            archiveArtifacts(artifacts: 'target/Nadia.jar', onlyIfSuccessful: true, fingerprint: true)
            archiveArtifacts(artifacts: 'target/Nadia*javadoc.jar', fingerprint: true)
            
          }

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.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

One thought on “Jenkinsfile for a Maven Build”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d