Integrating Maven with Jenkins

Integrating Maven with Jenkins allows Jenkins to automatically build Java projects using Maven commands such as:

  • clean
  • compile
  • package
  • test

This eliminates the need to execute Maven commands manually every time.

Analogy

Think of Maven as an instruction booklet that tells Jenkins which blocks to use.

Advertisement

What Maven Integration Does

Maven integration allows Jenkins to:

  • Invoke Maven goals and phases
  • Compile source code
  • Execute unit tests
  • Generate reports
  • Create artifacts such as .jar and .war files

These activities become part of the CI/CD pipeline.

Why It Matters for Testers

Maven integration:

  • Automates test execution
  • Generates deployment artifacts automatically
  • Creates reliable and repeatable Java builds

Maven Setup (Step by Step)

Step 1: Install Maven in Jenkins

Navigate to:

Manage Jenkins → Global Tool Configuration

Add a Maven installation.


Step 2: Create a Jenkins Job or Pipeline

Option A: Freestyle Job

Create:

New Item → Freestyle Project

Navigate to:

Build → Invoke top-level Maven targets

Specify Maven goals such as:

 
clean install test
 

Option B: Pipeline Job

Create a pipeline stage that executes the Maven goals from the Jenkinsfile.

Interview Line

"I installed Maven in Jenkins, configured the job to use Maven goals (clean install test), and set up the pipeline to pull code from Git and run the build."


The Purpose of pom.xml

pom.xml (Project Object Model) is the configuration file for a Maven project.

Analogy

Think of pom.xml as a recipe that lists the ingredients (libraries) and the tests required to prepare the project.

What pom.xml Defines

  • Project Dependencies
  • Build Plugins
  • Build Goals
  • Maven Lifecycle
  • Project Metadata

Examples of metadata include:

  • Version
  • artifactId
  • groupId

Why It Matters in Jenkins

pom.xml tells Jenkins how to:

  • Build the project
  • Execute tests
  • Package the application

It also ensures that project dependencies are downloaded automatically.

Without pom.xml, Jenkins would not know how to build or test the application.

Additional Note

  • pom.xml defines the Maven project.
  • settings.xml configures Maven itself (repositories, mirrors, credentials).

mvn install vs mvn package & Surefire Reports

mvn package

Performs the following:

  • Compiles source code
  • Executes tests
  • Packages the application

Creates an artifact such as:

  • .jar
  • .war

inside the target directory.


mvn install

Performs everything that mvn package does.

Additionally:

  • Installs the generated artifact into the local Maven repository.

This allows other Maven projects to use the artifact.


Maven Reports

The Surefire Plugin:

  • Executes tests
  • Generates test reports during the build

Jenkins can publish these reports as post-build results.

Examples include:

  • Surefire Reports
  • JaCoCo Reports

Parallel Test Execution

Maven tests can also run in parallel for multi-module projects to reduce build time.


Deploying Code with Jenkins

Deploying code with Jenkins means taking the generated application and deploying it to a target server such as:

  • QA
  • Staging
  • Production

Deployment automates activities that would otherwise be manual.

Typical deployment activities include:

  • Copying files
  • Executing scripts
  • Running deployment commands

Deployment Flow

  1. Jenkins pulls the latest source code from Git.
  2. Jenkins builds the application using Maven or Gradle.
  3. The deployment stage copies the generated artifact (.jar or .war) to the target server.
  4. The application is deployed to the selected environment.

Why It Matters for Testers

Deployment automation:

  • Makes builds available quickly in QA and Staging
  • Eliminates manual deployment errors
  • Enables faster delivery

Multiple Servers, Failed Deployments & Rollback

Deploy to Multiple Servers

The deployment stage copies the artifact to every required target server.

Alternatively, Jenkins executes deployment steps for each environment.


Handle Failed Deployments

When deployment fails:

  • Pipeline execution stops.
  • Jenkins notifies the team.
  • The Console Log is used for investigation.

Rollback a Deployment

Redeploy the previous stable artifact or application version to restore the last working deployment.


Manage Deployment Secrets

Use Jenkins Credentials instead of hardcoded values for:

  • Server Credentials
  • Authentication Information

Cloud Platforms

Jenkins can deploy applications to cloud platforms such as:

  • AWS
  • Azure

using the required plugins or deployment scripts.


FAQs

How Do You Integrate Maven with Jenkins?

  1. Configure Maven in Global Tool Configuration.
  2. Execute Maven goals using:
    • Invoke top-level Maven targets (Freestyle)
    • Pipeline stages (Jenkinsfile)

Example goals:

 
clean install test
 

What Is the Purpose of pom.xml?

pom.xml defines:

  • Dependencies
  • Plugins
  • Build Goals
  • Maven Lifecycle
  • Project Metadata

It tells Jenkins how to build, test, and package the application.


What Is the Difference Between mvn install and mvn package?

mvn package

  • Compiles
  • Tests
  • Creates the artifact inside target

mvn install

  • Performs all package tasks
  • Installs the artifact into the local Maven repository

How Do You Deploy Code Using Jenkins?

Typical deployment flow:

  • Pull code from Git
  • Build with Maven
  • Copy the generated artifact (.jar or .war)
  • Deploy to the target environment

How Do You Roll Back a Deployment?

Redeploy the previous stable artifact or application version to restore the earlier working deployment.


How Do You Generate Maven Test Reports in Jenkins?

The Surefire Plugin executes tests and generates reports during the build.

Jenkins publishes these reports as post-build results.