linux - Jenkins 在 Linux 或 Windows 上运行 Maven 构建

标签 linux windows maven jenkins jenkins-pipeline

我有一个 Java 应用程序的 Maven 构建,它通过键入相同的命令 mvn install 在 Linux 或 Windows 上成功运行。

但是,使用 Jenkinsfile method设置此构建,在 Linux 上该文件需要包含 sh mvn install 和在 Windows 上 bat mvn install

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

[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 允许在两种环境中构建?

最佳答案

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

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

关于linux - Jenkins 在 Linux 或 Windows 上运行 Maven 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39252654/

相关文章:

windows - 让 Vim 在 Windows 下与 Go 一起工作

python - Jenkins 执行时间太长

windows - 批处理 : delete line feed from end of text file using filepaths

使用 Maven 测试时 Spring Boot 应用程序未读取 application.properties 文件

python - 如何在 shell 命令中使用 python 跳过按 Enter 继续

linux - 编译时出错:::strcmp 尚未声明

Node.js:在使用 'fs' 遍历目录时检查文件是否为符号链接(symbolic link)

java.sql.SQLIntegrityConstraintViolationException : Column 'ITEM_QTY' cannot accept a NULL value

maven - 如何使用 Maven Surefire 插件调用 JUnit5 测试运行器/Junit 标记表达式?

linux - 如何从多核平台中的所有 CPU 获取调用跟踪