Jenkins 到 运行 Maven 在 Linux 或 Windows 上构建
Jenkins to run Maven build on Linux or Windows
我有一个 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 允许在两种环境中构建?
管道脚本可以包含 groovy 代码,因此允许使用 if
进行分支。您可以使用 System.properties['os.name']
测试您的环境,并根据结果使用 sh
或 bat
:
node {
def os = System.properties['os.name'].toLowerCase()
echo "OS: ${os}"
if (os.contains("linux")) {
sh "mvn install"
} else {
bat "mvn install"
}
}
我有一个 Java 应用程序的 Maven 构建,它通过键入相同的命令 mvn install
.
但是,使用 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 允许在两种环境中构建?
管道脚本可以包含 groovy 代码,因此允许使用 if
进行分支。您可以使用 System.properties['os.name']
测试您的环境,并根据结果使用 sh
或 bat
:
node {
def os = System.properties['os.name'].toLowerCase()
echo "OS: ${os}"
if (os.contains("linux")) {
sh "mvn install"
} else {
bat "mvn install"
}
}