Java Gradle:如何让任务一个接一个执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23880461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle: how to make tasks execute one after another?
提问by Zennichimaro
Basically I have 4 tasks that I need to run sequentially, but I cannot make them do so, I have to run it one by one on the command line as so:
基本上我有 4 个任务需要按顺序运行,但我不能让它们这样做,我必须在命令行上一一运行它,如下所示:
gradle :drmdexsecondary:compileReleaseJava --info --debug --stacktrace
gradle :drmdexsecondary:dexClasses --info --debug --stacktrace
gradle :drmdexsecondary:jar --info --debug --stacktrace
Here's my build.gradle:
这是我的 build.gradle:
evaluationDependsOnChildren();
评估DependsOnChildren();
task dexClasses( type:Exec ) {
// compileJava.execute()
String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
println("${buildDir}")
println("${androidSdkDir}\build-tools\${buildToolsVersion}\dx${cmdExt} --dex --output=${buildDir}\classes\classes.dex ${buildDir}\classes\release")
commandLine "cmd", "/c", "${androidSdkDir}\build-tools\${buildToolsVersion}\dx${cmdExt} --dex --output=${buildDir}\classes\classes.dex ${buildDir}\classes\release"
}
task jar(type: Jar) {
from ("${buildDir}\classes\classes.dex")
}
My problem is basically:
1. the dependsOn keyword doesn't work... it just get ignored without any log message
2. taskname.execute()
function doesn't work... it just get ignored without any log message
3. compileReleaseJava
is not recognized inside build.gradle with this particular error: Could not find property 'compileJava' on task ':drmdexsecondary:dexClasses'.
我的问题基本上是: 1.dependsOn 关键字不起作用......它只是被忽略而没有任何日志消息 2.taskname.execute()
函数不起作用......它只是被忽略而没有任何日志消息 3.compileReleaseJava
在构建中无法识别.gradle 这个特殊的错误:Could not find property 'compileJava' on task ':drmdexsecondary:dexClasses'.
Would anyone please help? I've consulted and copy paste from the documentation but none of them seems work. I've even tried to re-install gradle... there is so few sample codes there and although I understand the concept, it seems so difficult to translate my intention into working gradle code, so if there is any good resources to help, it will be very appreciated as well... Thanks!
有人会帮忙吗?我已经咨询并从文档中复制粘贴,但它们似乎都不起作用。我什至尝试重新安装 gradle ......那里的示例代码很少,虽然我理解这个概念,但将我的意图转化为工作 gradle 代码似乎很难,所以如果有任何好的资源可以提供帮助,它也将不胜感激...谢谢!
回答by ditkin
You should read about gradle tasksand more about tasks. You want to depend on things rather then invoke things.
你应该阅读关于gradle tasks和更多关于 tasks。你想依赖事物而不是调用事物。
Also I think you should read about, and use the gradle android plugin.
另外我认为您应该阅读并使用gradle android plugin。
To your original question, when you define a task the code between the braces is run at configuration time. A task's actions are run when the task is executed and must be added to the task's action list. This is done by using the task's doFirst, doLast or the << operator.
对于您的原始问题,当您定义任务时,大括号之间的代码在配置时运行。任务的操作在任务执行时运行,并且必须添加到任务的操作列表中。这是通过使用任务的 doFirst、doLast 或 << 操作符来完成的。
Here is an example from the gradle documentation.
这是 gradle 文档中的一个示例。
task taskX << {
println 'taskX'
}
task taskY << {
println 'taskY'
}
task taskZ << {
println 'taskZ'
}
taskX.dependsOn taskY
taskY.dependsOn taskZ
taskZ.shouldRunAfter taskX
回答by Sharif Mamun
For sequential or whatever the sequence you want to run your tasks, I do the following thing in my build.gradle file:
对于顺序或任何您想要运行任务的顺序,我在 build.gradle 文件中执行以下操作:
def modules = ["X", "Y", "Z", "ZZ"]
if (modules.size() > 1) {
for(j in 1 .. modules.size()-1 ) {
tasks[modules[j]].mustRunAfter modules[values[j-1]]
}
}
回答by tschumann
task1.finalizedBy task2
will run task2 after task1.
task1.finalizedBy task2
将在 task1 之后运行 task2。
回答by lixiaodaoaaa
The Best Answer is Here
最佳答案在这里
task myTask1() {
println("this is task1 running")
}
task task2() { dependsOn myTask1
println("this is task2 running")
}
when you execute gradle task2.
当你执行 gradle task2。
this wil first execute task1 ,and then execute task2
这将首先执行 task1 ,然后执行 task2
you should use dependsOn key words
你应该使用dependsOn关键字
For example
例如
task buildMyApk(type: Exec) {
def myCommond = ['gradle', "assembleRelease"]
commandLine myCommond
}
task moveApkFileToProjectFolder() { dependsOn buildMyApk
def releaseApkFilePath = "${buildDir}/outputs/apk/release/"
def targetApkFolder = "${rootProject.getProjectDir()}/apkFolder"
mkdir "${targetApkFolder}"
copy{
from releaseApkFilePath
into targetApkFolder
}
}
above answer will not work task moveApkFileToProjectFolder run ,will first run buildMyApk.
以上答案将无法运行任务 moveApkFileToProjectFolder 运行,将首先运行 buildMyApk。
my example task ,first will build apks .and then move apk File to my Project/apkFile folder .and execute success.
我的示例任务,首先将构建 apk。然后将 apk 文件移动到我的 Project/apkFile 文件夹中。并执行成功。