在Gradle中强制执行任务 [英] Force task execution in Gradle

查看:533
本文介绍了在Gradle中强制执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的一定量的Gradle任务,不需要任何输入或输出。因此,这些任务在我给他们打电话时总会得到状态 UP-TO-DATE 。例如:

 任务backupFile(类型:复制)<< {
//这两个参数都是从gradle.properties文件中读取的
从文件(adjusting_file.replaceAll(\,))
读入文件(backupDestinationDirectory +/ main /)

println[INFO]备份的主文件

结果如下:

$ p $ :gradle backupFile
:backupFile UP-TO-DATE






有没有办法 (ny)要执行的任务,不管任何事情
如果存在,是否也可以切换任务执行(例如告诉构建脚本运行哪些任务以及哪些任务忽略)?



我不能忽略<< 标签,因为这会使任务总是执行,这不是我想要的。



非常感谢您的输入。

解决方案

任务必须在 configur中配置阶段。但是,您正在将它配置为在执行阶段中运行的任务操作(<< {...} )。由于您配置任务的时间太晚,所以Gradle认为它没有任何功能,并打印 UP-TO-DATE



下面是一个正确的解决方案。同样,我推荐使用 doLast 而不是< ,因为它会导致更常规的语法,并且更少可能会被意外添加/省略。

  task backupFile(type:Copy){
from file(adjustments_file.replaceAll( \\ bb,))
到文件(backupDestinationDirectory +/ main /)
doLast {
println[INFO]备份的主文件
}
}


A certain amount of Gradle tasks I wrote, don't need any in- or output. Because of that, these tasks always get the status UP-TO-DATE when I call them. An example:

task backupFile(type: Copy) << {
    //Both parameters are read from the gradle.properties file
    from file(adjusting_file.replaceAll("\"", "")) 
    into file(backupDestinationDirectory + "/main/")

    println "[INFO] Main file backed up"
}

Which results in the following output:

:gradle backupFile
:backupFile UP-TO-DATE


Is there a way to force a(ny) task to execute, regardless of anything? If there is, is it also possible to toggle task execution (e.g. telling the build script which tasks to run and which tasks to ignore)?

I can't omit the << tags, as that would make the tasks to always execute, which isn't what I desire.

Many thanks in advance for your input.

解决方案

Tasks have to be configured in the configuration phase. However, you are configuring it in a task action (<< { ... }), which runs in the execution phase. Because you are configuring the task too late, Gradle determines that it has nothing to do and prints UP-TO-DATE.

Below is a correct solution. Again, I recommend to use doLast instead of << because it leads to a more regular syntax and is less likely added/omitted accidentally.

task backupFile(type: Copy) {
    from file(adjusting_file.replaceAll("\"", "")) 
    into file(backupDestinationDirectory + "/main/")
    doLast {
        println "[INFO] Main file backed up"
    }
}    

这篇关于在Gradle中强制执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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