Maven Installation and Integration with Jenkins

In this article, I will demonstrate the installation of the Apache Maven build tool and it’s integration with Jenkins server. Maven is a project management tool and mainly used for building Java-based projects. Maven is based on the Project Object Model (POM).

I hope you have already installed a Jenkins server if not please refer below article.

Jenkins server installation

Maven installation:

To download maven binary file, access the below link and click on binary tar.gz archive.

https://maven.apache.org/download.cgi?Preferred=ftp%3A%2F%2Fapache.cs.utah.edu%2Fapache.org%2F

 

Right click on copy the link and download it from the server.

Enter “/opt” folder and download it with wget command.

[root@jenkins ~]# cd /opt/
wget http://apachemirror.wuchna.com/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz

Untar the downloaded binary file and move to different name.

[root@jenkins opt]# ls

apache-maven-3.6.2-bin.tar.gz

[root@jenkins opt]# tar -xvzf apache-maven-3.6.2-bin.tar.gz 

[root@jenkins opt]# mv apache-maven-3.6.2 apache-maven

[root@jenkins apache-maven]# ls

bin  boot  conf  lib  LICENSE  NOTICE  README.txt

 

Next, set the path in the bash profile. For Maven we have to set two path once is for M2_home and is M2. Add below code in the root bash profile.

vi ~/.bash_profile

M2_HOME=/opt/apache-maven
M2=/opt/apache-maven/bin
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2:$M2_HOME

I added it along with my Java path, make changes based on your bash profile.

Log out and login from the current session and execute below command to make sure that the path is available.

[root@jenkins ~]# echo $M2

/opt/apache-mave/bin

[root@jenkins ~]# echo $M2_HOME

/opt/apache-maven

To check Maven version.

[root@jenkins ~]# mvn --version

Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T15:06:16Z)

Maven home: /opt/apache-maven

Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.47.amzn1.x86_64/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "4.14.138-89.102.amzn1.x86_64", arch: "amd64", family: "unix"

Integrate with Jenkins:

Login to the Jenkins server and select Manage Jenkins from the left panel.

Click on Manage plugin and select available. In the search box enter maven. It shows a list of maven plugins. Select Maven integration and Maven Invoker plugging from the list and click Install without restart button. Jenkins will start installing a lot of dependent plugins.

Create a Maven job

We have completed Maven integration with Jenkins, next we will create a job to build with Maven. For this login to the Jenkins server and click on “New item” on the left side. Give project and select Maven project.

On the next page add a description for the project and click on “Git” under the source code management. Add repository url of your project.

 

If you don’t have repository on Github you can use mine. To download my sample project click below link.

https://github.com/techiescorner/sample-maven-project.git

Or if you want to create a sample one please refer below article.

Sample maven project using Java code.

I have created this repository as a public repository so no need to give any credentials. Next, add Github branch details, my code is present in the master branch so I am using the same.

Before proceeding make sure that the repository contains POM.xml file. Also, add “clean install package” as Goal and options.

Click apply and then save.

To build the code click on “Build now” form the left side menu. To view the complete logs click on the “ball” icon under build history.

 

On the next window you could see the complete build output. If the build is successful it shows a message like below.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.247 s
[INFO] Finished at: 2019-11-29T15:47:57Z
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /var/lib/jenkins/workspace/First_maven_project/pom.xml to www.techiescorner.in/TC-maven/0.1.0/TC-maven-0.1.0.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/First_maven_project/target/TC-maven-0.1.0.jar to www.techiescorner.in/TC-maven/0.1.0/TC-maven-0.1.0.jar
channel stopped
Finished: SUCCESS

Jenkins save all works related files in folder called “Workspace” for each project. It copy all file from the Github to this folder.

We have completed Apache Maven installation and integrated with Jenkins server.

Leave a Reply

Your email address will not be published. Required fields are marked *