Building Maven Projects

Harry · 12 Sep 2026 · 14 views

Prerequisites in Jenkins

Add a JDK and Maven under Manage Jenkins → Tools. Name them, e.g. JDK21 and Maven3, and let Jenkins install them automatically or point at your installation paths. The names are used in jobs and pipelines.

Maven Build in a Freestyle Job

Add build steps:

1. Invoke top-level Maven targets
   Goal: clean package
2. Archive the artifacts
   Files to archive: target/*.jar, target/*.war

Maven Build in a Pipeline

pipeline {
    agent any
    tools {
        maven 'Maven3'
        jdk 'JDK21'
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B clean verify'
            }
        }
        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'target/*.jar'
            }
        }
    }
}

Running Tests and Checking Reports

  • Surefire creates XML reports in target/surefire-reports.
  • Use the Publish JUnit test result report step to plot results and fails the build on regressions.
  • Nine tests failed but the build passed? Add the JUnit step - Jenkins will mark the build UNSTABLE on test failures.

Key Points

  • Tools in Jenkins make JDK/Maven versions explicit and reproducible.
  • Archive artifacts so the built jar/war survives into later jobs.
  • Publish test reports to get pass/fail trends over time.
Share this post:

Comments (0)

Please login or register to comment.