Install and setup maven in Linux for Jenkins

In this post we will guide to install and setup Apache Maven in Linux system. The same Apache Maven you can also use in Jenkins for building Java projects. The steps written in this article are generic and you can follow the same steps in almost all Linux distros.

What is Apache Maven

Apache Maven is a build automation software. Primarily it is used in Java projects, it can also be used for building other language based projects like C#,ruby,scala. Apache Maven is based on the concept of POM (Project Object Model).
Apache Software Foundation hosts the Maven project hence it is known as Apache Maven.

Steps of Installing Apache Maven in Linux

Installation steps of Apache Maven is generic with almost all Linux/Unix distros.
If you are looking to setup Maven in Jenkins then follow the installation steps in your Jenkins Servers.

Download Apache Maven

Always download the latest and stable Apache Maven from it official websites.

wget http://mirrors.wuchna.com/apachemirror/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz

Create Maven installation directory

sudo mkdir -p /opt/maven

Extract the downloaded Maven binary file

sudo tar -xvzf apache-maven-3.6.0-bin.tar.gz -C /opt/maven/ --strip-components=1

Create symlink to make mvn command available to all users

sudo ln -s /opt/maven/bin/mvn /usr/bin/mvn

Now you can verify the absolute path of mvn command by using which command. Here is the output from our system.

user1@server01:~$ which mvn
/usr/bin/mvn
user1@server01:~$

You can also validate by using some parameter with mvn command. For example, mvn --version

user1@server01:~$ mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /opt/maven
Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /opt/java/jdk1.8.0_192/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-43-generic", arch: "amd64", family: "unix"
user1@server01:~$ 

Now you can call the mvn command from the application which can use the system. When you install Maven in Jenkins Server, you can directly call it from Jobs.

Using Maven In Jenkins (Without Plugin)

When you install the Apache Maven in Jenkins server, without Jenkins Maven Plugin you can build the java app. The given below screenshot will help you in this.

1. Create a new Jenkins Job.
maven jenkins job

2. Create new “Freestyle project”

maven jenkins

3. Give Git repo url of your Java application source code and related details. Use the git credential to access the git repo. (Learn more about “Jenkins Credentials”)

maven jenkins git

4. Now scroll down and reach to ‘Build’ section. Click on “Add build step” >> select “Invoke top-level Maven Targets”.

jenkins maven

5. Click on “Advanced” button in ‘Build’ section. It will show more options.

jenkins maven

6. Now here you can provide all the values related to Maven and your Java Project.

jenkins maven

This article comprising the basic information of setting Maven in Jenkins. Hope, it will help you in kickstart of your first Java application build. We will describe more about building Java app from Jenkins in our next article.

1 thought on “Install and setup maven in Linux for Jenkins”

Comments are closed.