Creating buildDir for a task

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.

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.

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.

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

1 Like