DEV Community

Khalif AL Mahmud
Khalif AL Mahmud

Posted on

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.

Top comments (0)