Creating buildDir for a task


(Taavi Ilves) #1

I have defined a task which generates some files which should go to build directory ($buildDir). Currently I have defined new task for this and trigger it with task dependency:

task createBuildDir << {
    mkdir project.buildDir
}

task ("generate", type:Exec, dependsOn:'createBuildDir') {...}

This works perfectly, but leaves me wondering if there is more concise Gradle-way for doing it.


(Stefan Oehme) #2

There is, you can use doFirst { mkdir ... } in your generate task.

Even better, you can write your task as a custom task class and annotate the output directory with @OutputDirectory and it will be created automatically.

Also, I suggest you don’t generate to the build folder directly, but to some subfolder inside there. This will keep the task’s outputs separate from other tasks’s outputs.


(Taavi Ilves) #3

Thank you for your answer.

doFirst {...} was a missing piece here. A custom task class is good alternative, but currently too big cannon for my use-case.

I also refactored my build to place generated files under separate directory in build.


(Lance Java) #4

outputs.dir(...) is equivelant to @OutputDirectory