Jenkins中配置gradle项目的坑

Posted Q博士

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins中配置gradle项目的坑相关的知识,希望对你有一定的参考价值。

今天在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

以上是关于Jenkins中配置gradle项目的坑的主要内容,如果未能解决你的问题,请参考以下文章

Jenkins+Gradle+Github实现Android自动化打包

利用jenkins+gradle完成Android项目打包

Jenkins+Gradle+Sonar进行Java项目代码分析

Jenkins自动部署springboot项目

jenkins,自动部署scp传输错误的坑

jenkins构建基于gradle的springboot项目CI采坑(采用jar方式部署)