Dynamically created task of type Copy is always UP-TO-DATE

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:19:07

A Copy task only gets executed if it has something to copy. Telling it what to copy is part of configuring the task, and therefore needs to be done in the configuration phase, rather than the execution phase. These are very important concepts to understand, and you can read up on them in the Gradle User Guide or on the Gradle Forums.

doFirst and doLast blocks get executed in the execution phase, as part of executing the task. Both are too late to tell the task what to copy: doFirst gets executed immediately before the main task action (which in this case is the copying), but (shortly) after the skipped and up-to-date checks (which are based on the task's configuration). doLast gets executed after the main task action, and is therefore clearly too late.

I think the following Gradle User Guide quote answers my question the best:

Secondly, the copy() method can not honor task dependencies when a task is used as a copy source (i.e. as an argument to from()) because it's a method and not a task. As such, if you are using the copy() method as part of a task action, you must explicitly declare all inputs and outputs in order to get the correct behavior.

Having read most of the answers to "UP-TO-DATE" Copy tasks in gradle, it appears that the missing part is 'include' keyword:

task copy3rdPartyLibs(type: Copy) {
    from 'src/main/jni/libs/'
    into 'src/main/libs/armeabi/'
    include '**/*.so'
}

Putting from and into as part of the doLast section does not work. An example of a working task definitions is:

task copyMyFile(type: Copy) {

    def dockerFile = 'src/main/docker/Dockerfile'
    def copyTo = 'build/docker'

    from dockerFile
    into copyTo

    doLast {
        println "Copied Docker file [$dockerFile] to [$copyTo]"
    }
}

Not the behavior I was expecting. Using gradle 3.2.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!