Thursday, 4 September 2025

How to integrate Maven with Jenkins to build Java Project

Step 1:- Download Maven from official sites:-  

https://maven.apache.org/download.cgi


[root@jenkins-server ~]# pwd

/root

[root@jenkins-server ~]# cd /opt/

[root@jenkins-server opt]# ls

[root@jenkins-server opt]# ls

[root@jenkins-server opt]# wget https://dlcdn.apache.org/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.tar.gz

--2025-09-05 04:46:31--  https://dlcdn.apache.org/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.tar.gz

Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644

Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.

HTTP request sent, awaiting response... 200 OK

Length: 9160848 (8.7M) [application/x-gzip]

Saving to: ‘apache-maven-3.9.11-bin.tar.gz’


apache-maven-3.9.11-bin.tar.gz            100%[===================================================================================>]   8.74M  --.-KB/s    in 0.08s


2025-09-05 04:46:32 (113 MB/s) - ‘apache-maven-3.9.11-bin.tar.gz’ saved [9160848/9160848]


[root@jenkins-server opt]# ls

apache-maven-3.9.11-bin.tar.gz


Step 2:- Once downloaded the untar it.


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

apache-maven-3.9.11/README.txt

apache-maven-3.9.11/LICENSE

apache-maven-3.9.11/NOTICE

apache-maven-3.9.11/lib/

apache-maven-3.9.11/lib/aopalliance.license

apache-maven-3.9.11/lib/asm.license

 


Step 3 :- Rename the folder name



[root@jenkins-server opt]# ls

apache-maven-3.9.11  apache-maven-3.9.11-bin.tar.gz

[root@jenkins-server opt]# mv apache-maven-3.9.11 maven

[root@jenkins-server opt]# ls

apache-maven-3.9.11-bin.tar.gz  maven

[root@jenkins-server opt]# cd maven/

[root@jenkins-server maven]# ls

LICENSE  NOTICE  README.txt  bin  boot  conf  lib

[root@jenkins-server maven]# cd bin/

[root@jenkins-server bin]# ls

m2.conf  mvn  mvn.cmd  mvnDebug  mvnDebug.cmd  mvnyjp


Run the manven command to check:-


[root@jenkins-server bin]# ./mvn -v

Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)

Maven home: /opt/maven

Java version: 21.0.8, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk

Default locale: en, platform encoding: UTF-8

OS name: "linux", version: "6.12.0-55.25.1.el10_0.x86_64", arch: "amd64", family: "unix"



Step 4:- Now set environment variable to run from anywhere 


[root@jenkins-server bin]# cd ~

[root@jenkins-server ~]# ls

'='

[root@jenkins-server ~]# ls -a

 .   ..   .bash_history   .bash_logout   .bash_profile   .bashrc   .cshrc   .ssh   .tcshrc   .wget-hsts  '='

[root@jenkins-server ~]# vi .bash_profile

[root@jenkins-server ~]# cat .bash_profile

# .bash_profile


# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

M2_HOME=/opt/maven

M2=/opt/maven/bin

JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.16.0.8-2.el9.x86_64

# User specific environment and startup programs


PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2


export PATH

[root@jenkins-server ~]# source .bash_profile

[root@jenkins-server ~]# echo $PATH

/root/.local/bin:/root/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-17-openjdk-17.0.16.0.8-2.el9.x86_64:/opt/maven:/opt/maven/bin

[root@jenkins-server ~]# mvn -v

Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)

Maven home: /opt/maven

Java version: 21.0.8, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-21-openjdk

Default locale: en, platform encoding: UTF-8

OS name: "linux", version: "6.12.0-55.25.1.el10_0.x86_64", arch: "amd64", family: "unix"

[root@jenkins-server ~]#



To find Java path run below command:-

[root@jenkins-server ~]# find / -name jvm
/usr/lib/jvm
/usr/share/jvm
/etc/jvm
[root@jenkins-server ~]# cd /usr/lib/jvm
[root@jenkins-server jvm]# ls
java                                      java-21-openjdk  jre-17-openjdk                           jre-openjdk
java-17                                   java-openjdk     jre-17-openjdk-17.0.16.0.8-2.el9.x86_64
java-17-openjdk                           jre              jre-21
java-17-openjdk-17.0.16.0.8-2.el9.x86_64  jre-17           jre-21-openjdk
[root@jenkins-server jvm]# ls -ltrh
total 0
lrwxrwxrwx. 1 root root 40 Aug 18 20:05 jre-17-openjdk-17.0.16.0.8-2.el9.x86_64 -> java-17-openjdk-17.0.16.0.8-2.el9.x86_64
drwxr-xr-x. 7 root root 97 Sep  3 07:27 java-17-openjdk-17.0.16.0.8-2.el9.x86_64
lrwxrwxrwx. 1 root root 21 Sep  3 07:27 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 29 Sep  3 07:27 jre-openjdk -> /etc/alternatives/jre_openjdk
lrwxrwxrwx. 1 root root 24 Sep  3 07:27 jre-17 -> /etc/alternatives/jre_17
lrwxrwxrwx. 1 root root 32 Sep  3 07:27 jre-17-openjdk -> /etc/alternatives/jre_17_openjdk
lrwxrwxrwx. 1 root root 26 Sep  3 07:27 java -> /etc/alternatives/java_sdk
lrwxrwxrwx. 1 root root 34 Sep  3 07:27 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root 29 Sep  3 07:27 java-17 -> /etc/alternatives/java_sdk_17
lrwxrwxrwx. 1 root root 37 Sep  3 07:27 java-17-openjdk -> /etc/alternatives/java_sdk_17_openjdk
drwxr-xr-x. 5 root root 68 Sep  3 10:12 java-21-openjdk
lrwxrwxrwx. 1 root root 24 Sep  3 10:12 jre-21 -> /etc/alternatives/jre_21
lrwxrwxrwx. 1 root root 32 Sep  3 10:12 jre-21-openjdk -> /etc/alternatives/jre_21_openjdk





Now install Maven plugins in Jenkins and configure path .

Also configure path of git and Java in Jenkins






If you have multiple java install and to select java 17 use below command:-




No comments:

Post a Comment