[英]How do you set up a property in a custom gradle task?
I want to write a task that takes a directory from
, does something with the files in it and writes the result into some other directory to
.我想编写一个任务,该任务
from
中获取目录,对其中的文件执行某些操作并将结果to
其他目录。
I've been led to believe this was the way to define such a task (kotlin dsl):我被引导相信这是定义这样一个任务的方式(kotlin dsl):
package my.app
abstract class FooBarTask : DefaultTask() {
@get:InputDirectory
abstract val from: Property<Directory>
@get:OutputDirectory
abstract val to: Property<Directory>
@TaskAction
fun doSomething() {
println("Hakuna Matata")
}
}
now how do I set the from
and to
value in a groovy-based build.gradle
?现在如何在基于 groovy 的
build.gradle
中设置from
和to
值?
def myTask = tasks.register('myTask', FooBarTask) {
from = layout.projectDirectory.dir("foo")
to = layout.buildDirectory.dir("bar")
}
this results in这导致
Could not create task ':my-subproject:myTask'.
> Please use the ObjectFactory.directoryProperty() method to create a property of type Directory.
and it shouldn't.它不应该。
How do you correctly define a directory property in a custom task?如何在自定义任务中正确定义目录属性?
Gradle has the specialized DirectoryProperty , that offers some additional functionality, compared to the plain Property<Directory>
which is one of the implemented interfaces. Gradle 具有专门的DirectoryProperty ,与作为实现接口之一的普通
Property<Directory>
相比,它提供了一些额外的功能。 So this specialized type should be used when declaring directory inputs/outputs.所以在声明目录输入/输出时应该使用这种特殊类型。
I'm actually not a 100% sure what caused the error you saw.我实际上不是 100% 确定是什么导致了您看到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.