Jenkins在Linux或Windows上运行Maven构建 [英] Jenkins to run Maven build on Linux or Windows

查看:443
本文介绍了Jenkins在Linux或Windows上运行Maven构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序的Maven构建,通过键入相同的命令mvn install,它可以在Linux或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.

但是,使用 Jenkinsfile方法在Linux上进行设置该文件需要包含sh mvn install并且在Windows bat 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.

如果在Windows上正确配置了路径和工具,日志将显示:

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

单个Jenkinsfile是否可以在两种环境中构建?

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

推荐答案

管道脚本可能包含常规代码,因此允许使用if进行分支.您可以使用System.properties['os.name']测试环境,并根据结果使用shbat:

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"
    }
}

这篇关于Jenkins在Linux或Windows上运行Maven构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆