Introduction
Modern software development relies on Continuous Integration (CI) and Continuous Delivery (CD) pipelines to automate building, testing, and deploying applications.
Maven integrates seamlessly with CI/CD tools such as Jenkins and GitHub Actions, making it easy to automate builds, run tests, and publish artifacts to repositories like Nexus and Artifactory.
In this lesson, you’ll learn:
- How to run Maven in Jenkins and GitHub Actions
- How to skip tests for faster builds (
-DskipTests) - How to deploy Maven artifacts to Nexus/Artifactory
1. Running Maven in Jenkins
Jenkins is one of the most widely used CI/CD tools, and Maven integrates with it out-of-the-box.
Example: Jenkins Pipeline with Maven:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/example/my-app.git'
}
}
stage('Build with Maven') {
steps {
sh 'mvn clean install'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
}
}
✅ Jenkins can automatically detect your pom.xml and run Maven commands.
✅ Build results, test reports, and artifacts can be stored and displayed in Jenkins.
2. Running Maven in GitHub Actions
GitHub Actions is a cloud-native CI/CD platform directly integrated with GitHub repositories.
Example: GitHub Actions Workflow with Maven:
Create .github/workflows/maven.yml in your repo:
name: Java CI with Maven
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Build with Maven
run: mvn clean install
✅ Automatically runs mvn clean install on every push or pull request.
✅ Caches dependencies for faster builds.
3. Skipping Tests for Faster Builds
In CI/CD pipelines, sometimes you want to build quickly without running unit tests (e.g., during prototyping).
Maven provides the -DskipTests flag:
mvn clean install -DskipTests
-DskipTests→ Compiles test code but does not run tests.-Dmaven.test.skip=true→ Skips compiling and running test code.
📌 Use this only when necessary — skipping tests in production pipelines is risky.
4. Deploying Artifacts to Nexus/Artifactory
Maven can automatically deploy build artifacts (JARs, WARs, POMs) to artifact repositories such as Sonatype Nexus or JFrog Artifactory.
Step 1: Configure distributionManagement in pom.xml
<distributionManagement>
<repository>
<id>releases</id>
<url>http://nexus.example.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.example.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Step 2: Add Credentials in settings.xml
Located at ~/.m2/settings.xml:
<servers>
<server>
<id>releases</id>
<username>deploy_user</username>
<password>deploy_password</password>
</server>
<server>
<id>snapshots</id>
<username>deploy_user</username>
<password>deploy_password</password>
</server>
</servers>
Step 3: Deploy Artifact
mvn clean deploy
✅ Uploads the artifact to Nexus/Artifactory
✅ Used in enterprise projects for sharing artifacts across teams
Best Practices for Maven in CI/CD
- Always run tests in production pipelines (skip tests only in dev builds).
- Cache Maven dependencies in CI/CD for faster builds.
- Use profiles (
dev,test,prod) to adapt builds to environments. - Keep credentials in CI/CD secrets, not hardcoded in
pom.xml.
Key Takeaways
- Maven integrates seamlessly with CI/CD tools like Jenkins and GitHub Actions.
- Use
-DskipTestsfor faster builds, but avoid skipping tests in production. - Artifacts can be deployed to Nexus or Artifactory using Maven’s
deployphase.