soru soru - 10 months ago 123
Linux Question

jenkins 2 run maven build on linux or windows

I have a maven build of a Java application that runs successfully on either Linux or Windows by typing the identical command 'mvn install'.

However, using the Jenkinsfile method of setting up this build, on Linux that file needs to contain

sh mvn install
and on windows
bat mvn install

If paths and tools are configured appropriately on Windows, log shows:

[Pipeline] sh
[C:\tools\jenkins\workspace\DSL\master] Running shell script
sh: C:\tools\jenkins\workspace\DSL\master@tmp\durable-60527040\ command not found

Is there any way for a single Jenkinsfile to allow building in both environments?


Pipeline scripts can contain groovy code, so branching with if is allowed. You can test your environment with[''] and depending on the result, use sh or bat:

node {
    def os =[''].toLowerCase()
    echo "OS: ${os}"
    if (os.contains("linux")) {
      sh "mvn install" 
    } else {
      bat "mvn install"