Gradle任务。 (类型:复制)“和< doLast>不能同时工作 [英] Gradle Task . "(type: Copy)" and <doLast> can't both work

查看:172
本文介绍了Gradle任务。 (类型:复制)“和< doLast>不能同时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 任务simpleTask {
print(simpleTask is reach);
}

任务copySomeFile(type:Copy){
print(copySomeFile is reach); baseProjectPath中的
;
导入toProjectPath;
appendXML();

def appendXML(){
//修改a.txt
}

//我只想运行simpleTask,但是当gradle simpleTask时,任务copySomeFile也会运行!



但如果这样写的话

 任务copySomeFile(类型:Copy)<< {
}

copySomeFile将无法工作。



它似乎是(type:Copy)无法与<<或doLast {}?



我只想--gradle simpleTask--gradle copySomeFile可以单独运行。

解决方案

您必须阅读 Gradle构建生命周期

您应该注意两个阶段 - 配置执行。所有任务总是在每个构建中进行配置,但只有其中一些被真正执行为执行阶段。



你看到的是 copySomeFile 任务是在配置阶段配置的。它不会复制任何内容,但必须进行配置。任务闭包中的所有内容都是任务的配置,这就是为什么您在输出中看到 print(copySomeFile is reach); 的结果。



<< doLast 用于在执行阶段执行某些操作,但是如果将所有配置放置到 doLast 部分或者添加< 到任务定义 - 这就是复制不起作用的原因。


task simpleTask{
    print("simpleTask is reach");
}

task copySomeFile(type: Copy){
    print("copySomeFile is reach");
    from baseProjectPath;
    into toProjectPath;
    appendXML();
}
def appendXML(){
    //modify a.txt
}

//i just want to run "simpleTask" only, but when "gradle simpleTask", the task"copySomeFile" will be run also ! I know beacuse gradle initialization.

but if write like this

task copySomeFile(type: Copy)<<{
}

the "copySomeFile" will not work.

it seems like "(type: Copy)" can't work with the "<<" or "doLast{}"?

i just want "--gradle simpleTask" "--gradle copySomeFile" can run alone.

解决方案

You have to read about Gradle build lifecycle.

There are 2 phases you should note - Configuration and Execution. All tasks are always been configured on every build, but only some of them are really executed as the Execution phase.

What you see is that copySomeFile task was configured during the configuration phase. It doesn't copy anything, but it has to be configured. And everything within a tasks closure is task's configuration, that is why you see results of the print("copySomeFile is reach"); in the output.

<< or doLast are used to run something at the Execution phase, but your task of type Copy will not be configured if you place all it's configuration into doLast section or add << to the task definition - that is the reason why copy doesn't work.

这篇关于Gradle任务。 (类型:复制)“和&lt; doLast&gt;不能同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆