0

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.

I've been led to believe this was the way to define such a task (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?

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?

User1291
  • 7,664
  • 8
  • 51
  • 108
  • 1
    Have you tried using https://docs.gradle.org/current/javadoc/org/gradle/api/file/DirectoryProperty.html instead of `Property` – Leonard Brünings Sep 24 '22 at 19:54
  • @LeonardBrünings that seems to have done the trick, thank you. if you make an answer out of it, could you briefly explain the difference between the two? – User1291 Sep 25 '22 at 10:45

1 Answers1

1

Gradle has the specialized DirectoryProperty, that offers some additional functionality, compared to the plain Property<Directory> which is one of the implemented interfaces. 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.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66