今天在jenkins配置gradle项目时遇到一个奇特的问题记录如下:

问题

在jenkins配置gradle项目如下所示:

这里写图片描述

我的目的是想执行gradle clean build run任务,但是构建的时候出现如下的错误:

[Gradle] - Launching build.
[workspace] $ /Users/wuxian/.jenkins/jobs/TestReportSender_test/workspace/gradlew clean build run
FATAL: command execution failed
java.io.IOException: Cannot run program "/Users/wuxian/.jenkins/jobs/TestReportSender_test/workspace/gradlew" (in directory "/Users/wuxian/.jenkins/jobs/TestReportSender_test/workspace"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at hudson.Proc$LocalProc.<init>(Proc.java:244)
    at hudson.Proc$LocalProc.<init>(Proc.java:216)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:803)
    at hudson.Launcher$ProcStarter.start(Launcher.java:381)
    at hudson.Launcher$ProcStarter.join(Launcher.java:388)
    at hudson.plugins.gradle.Gradle.performTask(Gradle.java:262)
    at hudson.plugins.gradle.Gradle.perform(Gradle.java:116)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:761)
    at hudson.model.Build$BuildExecution.build(Build.java:203)
    at hudson.model.Build$BuildExecution.doRun(Build.java:160)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:536)
    at hudson.model.Run.execute(Run.java:1741)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:374)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:184)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
    ... 16 more
Build step 'Invoke Gradle script' changed build result to FAILURE
Build step 'Invoke Gradle script' marked build as failure
Finished: FAILURE

ok,既然这个是默认调用gradlew的,我们就换一个方式,然后我选择了另一个选项,配置如下:

这里写图片描述

ok,我们构建项目,发现成功了。但是我们从输出中可以看出来,明显是调用了下面的配置,这就很明显配置串了。

[Gradle] - Launching build.
[workspace] $ gradle clean build run
:clean
:compileJava
:processResources
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build
:run
html已生成
邮件发送成功

BUILD SUCCESSFUL

后来搜索了一下这个问题,发现这个是bug。

总结

为了在jenkins中使用gradle,需要选择Invoke gradle script选项,然后还必须在Use gradle wrapper选项中配置你要跑的任务(但是你千万不能选择该选项,否则jenkins会默认调用gradlew程序执行任务,但是一般你配置gradle后,没有该程序,就会报错),才能成功的利用gradle程序构建你的程序。fuck。

其他问题

上面的问题在我的master机器上使用是可以的,但是移到slave机器上使用的时候就出现问题了。又报了一个错误:

[iOS_CI_step5_TestReportSender] $ gradle
FATAL: command execution failed
java.io.IOException: Cannot run program "gradle" (in directory "/Users/a58/jenkins/workspace/iOS_CI_step5_TestReportSender"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at hudson.Proc$LocalProc.<init>(Proc.java:244)
    at hudson.Proc$LocalProc.<init>(Proc.java:216)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:803)
    at hudson.Launcher$ProcStarter.start(Launcher.java:381)
    at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1136)
    at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1101)
    at hudson.remoting.UserRequest.perform(UserRequest.java:121)
    at hudson.remoting.UserRequest.perform(UserRequest.java:49)
    at hudson.remoting.Request$2.run(Request.java:324)

所以我放弃了,我直接使用shell命令吧…

source ~/.bash_profile
gradle clean run

然后又好使了:

+ gradle clean run
:clean
:compileJava
:processResources
:classes
:run
html已生成
邮件发送成功

BUILD SUCCESSFUL

Total time: 11.095 secs
Finished: SUCCESS
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐