1

I have the following pipeline script in Jenkins:

node {

  withMaven(globalMavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml', jdk: 'JDK 1.8.0u92', maven: 'apache-maven-3.2.2', mavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml') {

    sh '/my/path/apache-maven-3.2.2/bin/mvn clean install'
  }
}

For this, I am getting:

nohup: failed to run command `sh`: No such file or directory
ERROR: script returned exit code -2

Why is this?

I am sure that the path to my Maven installation is correct. When I run a job without the pipeline, Maven builds with no errors and I can see that it uses the same path.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
bsky
  • 19,326
  • 49
  • 155
  • 270

4 Answers4

7

This might be the result of modifying PATH.

Check your script and Global Properties and remove modifications to PATH. It is now recommended to use PATH+extra instead. It would still be picked up, but without breaking actual PATH.

Related issue on Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-41339

Majki
  • 618
  • 7
  • 14
3

In the end, I used shell instead of sh and it worked. No idea why, they don't have a proper API.

bsky
  • 19,326
  • 49
  • 155
  • 270
1

I would suggest to use it like this:

  withMaven(
            maven: 'M3',
            mavenSettingsConfig: 'maven-settings-for-the-task',
            mavenLocalRepo: '.repository') {

        // Run the maven build
        sh "mvn clean install"
    }

Apart from that I would not use absolute paths to global settings.xml nor to user settings.xml. I would prefer using usage of "Config File Provider Plugin" which has the advantage to have the settings.xml on Master and available on all nodes.

See also the documentation: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Maven+Plugin

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • One thing is that if I put `maven: 'M3'`, then it would say: `ERROR: Could not find 'M3' maven installation.` – bsky Feb 08 '17 at 19:06
  • 1
    You need to define Maven within the Jenkins Tools Configuration appropriate. – khmarbaise Feb 08 '17 at 19:11
  • I already have set up Maven in `configureTools`. I selected `Install automatically` and `Version: 3.2.2`. Don't you think Jenkins should see this version of Maven? – bsky Feb 08 '17 at 19:18
1

This error comes when you are trying to run script copied from windows machine to unix machine. YOU need to change the format to unix using : dos2unix <scriptname.sh> and the run your script in unix ./<scriptname.sh>