Jenkins Maven and Gradle Builds

Java projects rely on build tools to compile code, run tests, and package results. Maven and Gradle lead that space. Jenkins runs both tools on every code change and reports the outcome in one place. Mastering these two integrations covers a large share of real-world Jenkins work.

The Layman's View

A build tool works like a recipe card. The card lists ingredients (libraries), steps (compile, test, package), and the final dish (a JAR or WAR file). Jenkins plays the cook who follows the card every time someone changes an ingredient.

Maven in Brief

Maven reads a file named pom.xml. The file names the project, lists its dependencies, and sets the build rules. Maven follows a fixed life cycle, so every Maven project builds in a familiar way.

validate --> compile --> test --> package --> verify --> install --> deploy

Useful Maven Commands

CommandAction
mvn cleanDeletes the previous build output
mvn testCompiles code and runs unit tests
mvn clean packageBuilds a fresh package after tests pass
mvn -B clean verifyRuns in batch mode with quiet logs for servers

Gradle in Brief

Gradle reads build.gradle or build.gradle.kts. It describes the build as tasks that depend on each other. Gradle skips tasks whose inputs have not changed, which saves time on large projects.

Useful Gradle Commands

CommandAction
./gradlew cleanDeletes build output
./gradlew testRuns tests
./gradlew buildCompiles, tests, and packages

The gradlew script is the Gradle wrapper. Teams commit it to Git so every machine uses the same Gradle version.

Running Maven in a Freestyle Job

  1. Install the Maven Integration plugin if your Jenkins lacks it.
  2. Configure a Maven version under Manage Jenkins and Tools.
  3. Create a freestyle job and connect the Git repository.
  4. Add the build step Invoke top-level Maven targets.
  5. Choose the Maven version and enter the goals: clean package.
  6. Save the job and click Build Now.

For Gradle, add the step Execute shell and type ./gradlew build.

Running Both in a Pipeline

pipeline {
    agent any
    tools { maven 'Maven3' }
    stages {
        stage('Build with Maven') {
            steps {
                sh 'mvn -B clean package'
            }
        }
        stage('Build with Gradle') {
            steps {
                sh './gradlew build'
            }
        }
    }
    post {
        always {
            junit '**/target/surefire-reports/*.xml'
        }
    }
}

The junit step reads the test reports and shows a pass and fail graph on the job page. Gradle writes its reports to build/test-results/test, so a Gradle project uses the path '**/build/test-results/test/*.xml'.

What Happens During a Build

Jenkins agent
  |
  +-- clone repository
  |
  +-- download dependencies ---> Maven Central
  |
  +-- compile source files
  |
  +-- run unit tests ---> test reports
  |
  +-- package ---> target/app.jar
  |
  +-- archive and report to Jenkins

Speeding Up Builds

Reuse Downloaded Libraries

Maven stores libraries in the .m2 folder and Gradle uses .gradle. A persistent agent keeps these folders between builds, so later builds download almost nothing.

Use Parallel Builds

Maven accepts the flag -T 2C, which runs two threads per CPU core on multi-module projects. Gradle accepts --parallel.

Skip Work That Is Not Needed

A quick check build can use -DskipTests, but a release build must run every test.

Choosing Between the Two

PointMavenGradle
Config filepom.xml (XML)build.gradle (Groovy or Kotlin)
StyleFixed life cycleFlexible tasks
Speed on repeat buildsGoodVery good with caching
Learning curveGentleModerate

Troubleshooting Build Failures

  • Read the first error in the console, since later errors often follow from it.
  • Run the same command on your own machine to confirm the failure is not Jenkins specific.
  • Check that the JDK version in Jenkins matches the version the project needs.
  • Delete the workspace and rebuild when stale files cause odd results.

With a clean Maven or Gradle setup, Jenkins turns each commit into a tested package that the rest of your pipeline can use.