Overview
If you want to use a Jenkins pipeline to build your maven project in a clean docker container, you can do so as shown below.
You can do any maven commands you like. In this particular case I am:
- Setting the version in the POM to the one provided by the person running the Jenkins job.
- Building and deploying the maven project to Artifactory.
This is the “build one” of “build once deploy everywhere”.
To test this though, you can just swap my commands out for maven –version or something simple like that.
Configuration
Create a new pipeline job and:
- Add a string parameter called VERSION_TO_TAG.
- Set pipeline definition to “Pipeline script from SCM”.
- Give your repo checkout URL – e.g. ssh://git@url.mycompany.com:7999/PROJECT/repo-name.git.
- Specify your git credentials to use.
- Specify your Jenkinsfile path (It is probably literally just “Jenkinsfile” which is the default if you have the Jenkinsfile in the root of your repo).
Make sure your project has a “Jenkinsfile” at its root with a definition like this:
pipeline { agent { docker { image 'maven:3-alpine' } } stages { stage('Set version, build, and deploy to artifactory.') { steps { sh 'mvn versions:set -DnewVersion=$VERSION_TO_TAG' sh 'mvn deploy' } } } }
Now, when you build your project, you should see it docker-pull a maven:3-alpine image, start the container, and run our maven commands and upload artifacts to the Artifactory repository you have set up in your maven POM (in your distributionManagement section in case you haven’t done that yet).