简体   繁体   English

如何在自定义 gradle 任务中设置属性?

[英]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中设置fromto值?

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.

相关问题 如何使用 Gradle 在自定义 jar 任务中实现工件? - How do I use Gradle to implement an artifact in the custom jar task? 如何在 build.gradle.kts 中设置 gradle 任务以在 Kotlin(1.4) 多平台项目中创建 fatJar - how to set up a gradle task to create fatJar in Kotlin(1.4) Multiplatform project in build.gradle.kts 如何使用Gradle Kotlin DSL设置codecov? - How do I set up codecov with Gradle Kotlin DSL? 使用Kotlin实施插件时,如何扩展Gradle的任务? - How do you extend a task for Gradle when using Kotlin to implement a plugin? 您如何设置Kotlin多平台构建? - How do you set up a Kotlin multi-platform build? 如何在 kotlin 代码中获得在 &#39;gradle.properties 中定义的自定义属性? - How do I get a custom property defined in 'gradle.properties in kotlin code? 获取任务的属性? 在 Gradle kotlin - Get a property of task? in Gradle kotlin 执行自定义gradle任务时如何省略代码检查 - How to ommit codecheck when executing custom gradle task 如何在gradle中创建自定义任务以将java和kotlin代码打包到jar? - How to create a custom task in gradle to pack java and kotlin code to a jar? 如何从自定义 Gradle 任务中访问“JavaToolchainSpec” - How to access `JavaToolchainSpec` from within custom Gradle task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM