Logo

dev-resources.site

for different kinds of informations.

Integrating Maven with Jenkins: A Step-by-Step Guide

Published at
1/14/2025
Categories
devops
jenkins
Author
khalifalmahmud0
Categories
2 categories in total
devops
open
jenkins
open
Author
15 person written this
khalifalmahmud0
open
Integrating Maven with Jenkins: A Step-by-Step Guide

This guide walks you through integrating Maven with Jenkins to automate the build process for Java projects. We'll cover everything from setting up Maven on the Jenkins server to building a Java project hosted on GitHub.

1. Setting Up Maven on the Jenkins Server

Download and Install Maven

  • Switch to the root user to ensure you have the necessary permissions.
sudo su -
Enter fullscreen mode Exit fullscreen mode
  • Go to the /opt directory, which is typically used for storing applications on Linux.
cd /opt
Enter fullscreen mode Exit fullscreen mode
  • Visit maven.apache.org to get the latest Maven tar.gz file. Alternatively, use wget to download directly:
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
Enter fullscreen mode Exit fullscreen mode
  • Extract the Archive:
tar -xvzf apache-maven-3.9.9-bin.tar.gz
mv apache-maven-3.9.9 maven
Enter fullscreen mode Exit fullscreen mode

Verify Maven Installation

Before proceeding with setting up the environment variables, let's verify that Maven is correctly installed. Navigate to the Maven folder and check the version:

cd maven
./bin/mvn -v
Enter fullscreen mode Exit fullscreen mode

This will show the Maven version, Java version, and operating system details.

Note: Running mvn -v will only work if you're inside the /opt/maven/ folder. For access it globally we have to set environment variable.

Configure Environment Variables

Now that Maven is installed, we need to set up environment variables (JAVA_HOME, M2_HOME, and M2) to make Maven accessible globally on the system.

  • Find the Java Home Path If you don't already know where Java is installed, you can use the find command to locate it:
find / -name java-17*
Enter fullscreen mode Exit fullscreen mode

Once you find the Java installation path, note it down. For example, the path might be /usr/lib/jvm/java-17-openjdk

  • Set Environment Variables

=> Edit the .bash_profile file to add the necessary environment variables.

nano ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

=> Add the following lines at the end of the file, replacing the paths with the correct ones for your setup:

JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64
M2_HOME=/opt/maven
M2=/opt/maven/bin
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2

Enter fullscreen mode Exit fullscreen mode
  • JAVA_HOME points to the Java installation directory.
  • M2_HOME points to the directory where Maven is installed.
  • M2 points to the Maven bin directory, which contains the mvn command.
  • We update the PATH variable to include Maven and Java locations, ensuring that Maven can be accessed globally.

=> Save the file and exit the editor.

=> Reload the profile to apply the changes:

source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

=> Verify that Maven is now globally accessible by running:

mvn -v
Enter fullscreen mode Exit fullscreen mode

This should show Maven's version along with the Java version.

2. Install the Maven Plugin in Jenkins

  • Log in to Jenkins:
    Open your Jenkins dashboard in a web browser.

  • Install the Maven Plugin:

    • Navigate to Manage Jenkins > Manage Plugins.
    • Under the "Available" tab, search for "Maven Integration Plugin."
    • Select and install the plugin, then restart Jenkins if required.
  • Configure Maven and Java:

    • Go to Manage Jenkins > Global Tool Configuration.
    • Scroll to the "Maven" section and add a new Maven installation: Provide a name (e.g., Maven3.8) and Set the Maven home directory to /opt/maven.
    • Scroll to the "JDK" section and set the Java home directory (e.g., /usr/lib/jvm/java-11-openjdk).

3. Build a Java Project with Jenkins

Now that Maven is configured in Jenkins, we can set up a Jenkins job to build a Java project.

  • Create a New Maven Project

    • From the Jenkins dashboard, click on New Item.
    • Select Maven Project and give it a name (e.g., "JavaBuild").
    • Click OK.
  • Configure Git Repository and Build Goals

    • Under the Source Code Management section, select Git and provide the repository URL: https://github.com/devops-vsc/hello-java.git
    • Under the Build section, add the following goal: clean install

clean: Cleans the project by deleting the target directory.
install: Compiles the code, runs tests, and installs the artifacts in the local Maven repository.

  • Save the job configuration

    • Build the Project Click on Build Now to trigger the build process. Jenkins will fetch the code from the Git repository, compile it, and run the tests. If successful, the build will complete with a "SUCCESS" status.

Integrating Maven with Jenkins simplifies the build and deployment process for Java projects. By following this guide, you’ve set up Maven, configured Jenkins, and built a Java project from a GitHub repository. This setup provides a solid foundation for automating your software development workflows.

jenkins Article's
30 articles in total
Favicon
Mastering Cost Optimisation with Shell Scripting: Automate Log Storage in S3 for Budget-Friendly Infrastructure
Favicon
Can’t access username or password, forgot these credentials after installing Jenkins
Favicon
Deploying a Next.js UI App on S3 Using Jenkins🤩
Favicon
How to install Jenkins in ubuntu
Favicon
Integrating Maven with Jenkins: A Step-by-Step Guide
Favicon
"Is Jenkins better than Docker? Or are they meant for different purposes?"
Favicon
Can a Python Server (Serving HTML with Jinja2) Interact and Modify Files in a Jenkins Pipeline?
Favicon
Streamlining CI/CD: A Complete Guide to Installing Jenkins on AWS EC2
Favicon
[Boost]
Favicon
Best Practices of Optimizing CI/CD Pipelines: Jenkins Consultancy
Favicon
deploy Jenkins using docker compose with production ready
Favicon
Pipeline CD en Jenkins para terraform AWS EKS
Favicon
[Boost]
Favicon
Mastering Jenkins: A Step-by-Step Guide to Setting Up and Supercharging Your CI/CD Workflow
Favicon
13 Best DevOps Tools for Enhancing Business Processes
Favicon
A Complete Guide to Setting Up Nexus (2 Ways) + How to Connect Nexus to Jenkins
Favicon
Automating Kubernetes Deployments with CI/CD Pipelines (GitLab, Jenkins)
Favicon
Accelerate Releases with Shift-Left Validation: A Custom CI/CD Configuration Framework
Favicon
Ci CD pipeline with Jenkins
Favicon
Building a Secure and Scalable CI/CD Pipeline for EKS Using Jenkins and GitHub Actions
Favicon
Strategies for Improving Jenkins Pipeline Performance: Best Practices and Implementation
Favicon
Connecting Jenkins to Slack: A Beginner's Guide
Favicon
From 41 Minutes to 8 Minutes: How I Made Our CI/CD Pipeline 5x Faster
Favicon
Jenkins Guide for Beginners
Favicon
DevOps and Security: How To Build Resilient Pipelines
Favicon
Cómo instalar Jenkins en AWS: Guía paso a paso
Favicon
Automating Docker Workflows with Jenkins: A Complete Guide
Favicon
Top 5 Free DevOps Certification Courses to Boost Your Career!
Favicon
Jenkins with PHP – Run Your First Pipeline
Favicon
How to Manage Terraform with Jenkins

Featured ones: