Reputation: 329
I've installed Maven package on both Master and Slave
mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 1.8.0_282, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-67-generic", arch: "amd64", family: "unix"
And set M2_PATH
root@jenkins-slave-02:~# cat /etc/profile.d/maven.sh
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
But when invoke mvn in the pipeline, it gives me this
+ mvn compile
/jenkins/workspace/simple_any_tests_master@2@tmp/durable-1dedcd3b/script.sh: 1: mvn: not found
Of Course, nothing wrong with permission
drwxr-xr-x 6 root root 4096 Mar 24 08:25 apache-maven-3.6.3
lrwxrwxrwx 1 root root 23 Mar 24 08:25 maven -> /opt/apache-maven-3.6.3
Also tried with Freestyle project with these shell command
export M2_HOME=/opt/maven
export PATH=$PATH:$M2_HOME/bin
mvn -version
the error was gone
+ export M2_HOME=/opt/maven
+ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/maven/bin
+ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 1.8.0_282, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-67-generic", arch: "amd64", family: "unix"
Finished: SUCCESS
Once remove the export and run the freestyle project again
#export M2_HOME=/opt/maven
#export PATH=$PATH:$M2_HOME/bin
mvn -version
the error occurs
+ mvn -version
/tmp/jenkins2489726055191874806.sh: 6: mvn: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Tried with build-in Maven plugin and set Tool location for M3 in Node Properties doesn't help!
Upvotes: 1
Views: 824
Reputation: 1140
I used jenkins own tools section to install the maven dynamically on agents. This allows me to run jobs with different versions of them and control all agents in a single place.
Manage jenkins > global tool configuration > Maven Installations
In the Jenkinsfile I have:
pipeline {
agent {
label linux-small
}
environment {
PROJECT_VERSION = "${params.projectVersion?: mvn.version}"
}
tools {
maven params.mavenVersionSetAsParameterInJob
jdk params.javaVersionSetAsParameter
// most of the tools can be controlled this way
}
stages {....}
}
Upvotes: 1