"Mvn" command not recognized in Jenkins Windows slave node

I am trying to create a Java project on the Jenkins node slave and when I try to run any command mvn

on the slave from the Jenkins service I get the error: command could not be found

java.io.IOException: Cannot run program "mvn": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
...

      

NOTE. ... If I login directly, running any command mvn

works fine.

Windows Slave Node Setting

Maven is installed to directory D:\Apache\Maven

.

Set system environment variables:

  • JAVA_HOME

    = C:\Program Files\Java\jdk1.8.0_40

  • M2

    = %M2_HOME%\bin

  • M2_HOME

    = D:\Apache\Maven

The system environment variable is PATH

set to%M2%;...

On the Jenkins master web page, if I run the Groovy script println System.getenv("PATH")

on a Windows slave node, I can see that this has the correct, extended path for the Maven directory bin

.

I can also verify that the Maven directory bin

can be seen by doing the following:

Groovy Script: println "ls -al D:/Apache/Maven/bin".execute().text

total 14
drwxr-xr-x    8 D-AUN-00 Administ     4096 Apr 20 12:35 .
drwxr-xr-x    9 D-AUN-00 Administ     4096 Mar 31 11:46 ..
-rw-r--r--    1 D-AUN-00 Administ      230 Mar 31 11:46 m2.conf
-rwxr-xr-x    1 D-AUN-00 Administ     7085 Apr 20 11:49 mvn
-rw-r--r--    1 D-AUN-00 Administ     6007 Mar 31 11:46 mvn.cmd
-rwxr-xr-x    1 D-AUN-00 Administ     1796 Mar 31 11:46 mvnDebug
-rw-r--r--    1 D-AUN-00 Administ     1513 Mar 31 11:46 mvnDebug.cmd
-rwxr-xr-x    1 D-AUN-00 Administ     1843 Mar 31 11:46 mvnyjp

      

Curiously, if I start Maven with mvn.cmd

, then Maven works just fine:

Groovy Script: println "mvn.cmd --version".execute().text

Apache Maven 3.3.1 (cab6659f9874fa96462afef40fcf6bc033d58c1c; 2015-03-13T15:10:27-05:00)
Maven home: D:\Apache\Maven
Java version: 1.8.0_40, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_40\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"

      

Question: Is there something that I am missing or not configured correctly that would result in the command mvn

not being recognized?


EDIT: My assembly uses a custom Python bash script to call Maven. script is also configured to run builds for projects in other languages (PHP, Ruby, .NET, Node ), and tools for all of these collectors work very well ( composer

, bundle

, devenv.com

), however, I would have thought that the performance of the assembly does not matter if the console The script for the Node slave returns the same error when trying to run commands mvn

.


EDIT 2: The more I do this, the less I feel like it has anything to do with Jenkins and more to do with how shell scripts are executed on Windows. When I use a console script to call a Groovy script in a slave node, Jenkins just writes this to a temporary file on the slave Node and uses sh -xe <script>

it to call it. When I try a similar approach directly on a Windows machine, every script command (like mvn

, composer

) doesn't run from a shell script unless I add the command withsh

... This is unfortunate because the scripts I run may run on Linux or Mac Node in the future, and I would rather not interfere with it with Windows specific commands. I've tried this with Git bash and Msysgit and get the same results. Then I'll try Cygwin to see if it works ...

+3


source to share


3 answers


So, it turns out that it has to do with how shell commands are invoked in Windows in the context of various tools.

Jenkins Script Console and Groovy

In Jenkins, when using the Windows console, node Script Groovy Script will invoke script commands as if they were a Win32 or BATCH Script application (for example, "mvn --version".execute()

won't work because mvn

- it's a Script wrapper - "mvn.cmd --version".execute()

because it's mvn.cmd

a BATCH script).

When running Groovy Script commands on Windows node, commands using shell scripts need to be added with sh

to be explicit. This assumes that your Windows node has the path to the shell executable in its PATH.

An example Groovy Script that works when called on a Windows node:

println "sh mvn --version".execute().text

      



Python for Windows

I also realized that my Python build Script was suffering from some similar problem. If you try to run Python Script on Windows that calls the Script wrapper through it subprocess

, it won't work unless you include it shell=True

in the call. Alternatively, you can add sh

to the command to explicitly invoke the shell

NOTE. ... This will not work for some tools like bundle

Ruby - for these commands, shell=True

it would seem to work.

An example Python Script that works when called in a shell environment on Windows:

#!/usr/bin/env python
import subprocess
subprocess.call("mvn --version", shell=True)

      

0


source


The maven installation folder must be the same for both the master and Jenkins client. Check if the folder location on the master and subordinate is the same.



Other than that your env. properties are correct, but check if you added properties under custom or system properties. If you added properties under user and running Jenkins slave as windows service, I don't think Jenkins is picking up the values.

0


source


I solve this problem, add "#! / Usr / bin / env zsh --login" in the "Run shell window." It's very simple.

0


source







All Articles