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

前端 未结 4 589
一整个雨季
一整个雨季 2020-12-16 22:48

I\'ve prepared a very simple script, that illustrates the problem I see using Gradle 1.7 (need to stick with it because of some plugins not yet supporting newer versions).

相关标签:
4条回答
  • 2020-12-16 23:27

    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.

    0 讨论(0)
  • 2020-12-16 23:28

    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'
    }
    
    0 讨论(0)
  • 2020-12-16 23:36

    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

    0 讨论(0)
  • 2020-12-16 23:41

    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.

    0 讨论(0)
提交回复
热议问题