Jenkins to run Maven build on Linux or Windows

与世无争的帅哥 提交于 2019-12-12 10:55:35

问题


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\script.sh: command not found

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


回答1:


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

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


来源:https://stackoverflow.com/questions/39252654/jenkins-to-run-maven-build-on-linux-or-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!