1
votes

I have installed Jenkins and Maven separately in my Linux server. Maven is working from prompt. I tested by giving mvn -version and it gave below output:

Apache Maven 3.2.5 
Maven home: /data1/SOA/app/oracle/product/middleware/Oracle_Home/oracle_common/modules/org.apache.maven_3.2.5
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /data1/setup_files/JDK1.8/jdk1.8.0_151/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "3.8.13-55.1.6.el7uek.x86_64", arch: "amd64", family: "unix"

But from Jenkins when I try to give a maven build from Jenkins with below shell script:

export M2_HOME=/data1/SOA/app/oracle/product/middleware/Oracle_Home/oracle_common/modules/org.apache.maven_3.2.5
export PATH=$PATH:$M2_HOME/bin
echo "Java Version"
java -version
echo "Maven Version"
mvn -version

It throws following error:

+ echo 'Java Version'
Java Version
+ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
+ echo 'Maven Version'
Maven Version
+ mvn -version
/tmp/jenkins6612268406133694375.sh: line 7: mvn: command not found
Build step 'Execute shell' marked build as failure

I gone through several links and blogs, but none of the steps are helping. I have done the following:

In Manage Jenkins --> Configuration --> Environment variables --> set JAVA_HOME, M2_HOME, ORACLE_HOME, PATH

In Manage Jenkins --> Global Tool Configuration --> set JAVA_HOME and MAVEN_HOME to installation directory in server

I also restarted Jenkins and checked, but still same issue.

3
I would look into the PATH configuration of your Jenkins installation. Compare this to the PATH value in your own shell. I guess that the installation path of maven is only in your shell PATH variable.JBWanscher
This is how I did the PATH configuration in Jenkins. From shell I ran echo $PATH and the value I got , I pasted in PATH configuration of Jenkins installation at: Manage Jenkins --> Configuration --> Environment variables -->Path.Marsha
In Jenkins you have a part Global Tool Configuration where you should define the tools you have available and not via Path of the Machine on wich Jenkins is running...khmarbaise
I have already added the Maven installation path in Global Tool Configuration but still getting mvn not found error. But There is a warning in yellow appearing under Global Tool Configuration --> Maven Installation ---> MAVEN_HOME. The warning is /data1/SOA/app/oracle/product/middleware/Oracle_Home/oracle_common/modules/org.apache.maven_3.2.5 is not a directory on the Jenkins master (but perhaps it exists on some agents)Marsha
Could you try to run a “which mvn” in your own shell? That could tell you if your $PATH append in the script above actually points to the mvn executable. Do you know if you have multiple maven installations on the host?JBWanscher

3 Answers

3
votes

are you using a Jenkinsfile for your pipeline? If so try adding the following part to your pipeline script:

    tools {
        maven 'Maven 3.2.5'
    }

Thats doing to the trick in our project

2
votes

The execute shell does not know about the Maven settings; only the maven job type or step does.

Assuming Jenkins 2.x, Under Jenkins | Manage Jenkins | Global Tool Configuration, there's a section called "Maven" - Maven installations

click "Add Maven"

Name: "Maven-3.2.5" MAVEN_HOME:/data1/SOA/app/oracle/product/middleware/Oracle_Home/oracle_common/modules/org.apache.maven_3.2.5

Your freestyle job should then have an "Invoke top-level Maven targets" step.

Set "Maven Version": "Maven-3.2.5".

You can have shell steps prior to and after to deal with any non-maven steps in your job.

0
votes

I have the same problem, but now I have resolve it. Now I share the way.

when start docker container of jenkins,please execute follow commandline:

「docker run -d -v jenkins_home:/var/jenkins_home -v /usr/local/java/jdk1.8.0_281/:/usr/local/java/jdk1.8.0_281 -v /usr/local/apache-maven-3.6.3/:/usr/local/apache-maven-3.6.3 --name jenkins -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts」

Mapping your local linux environment of jdk and maven to the jenkins container, you can try it.