Is there a way for a task to depend on the existence of ${project.buildDir}?


(Emil Sit) #1

Sometimes I write custom tasks that generate things that would belong inside ‘build’. However, if I need to run these tasks early in the build cycle (say, before ‘compileJava’), I will need to add some sort of custom mkdir task to ensure that the directory exists.

This seems sort of icky to me. Is there a way for a task to depend on the existence of ‘${project.buildDir}’ without my making an explicit task for it?


(Peter Niederwieser) #2

If you declare the directory as an output of the task, it will be created automatically if it doesn’t exist. See 15.8.1. Declaring a task’s inputs and outputs in the Gradle User Guide. If you write a task class, you can also use the ‘org.gradle.api.tasks.OutputDirectory’ annotation.


(Emil Sit) #3

Hm. I don’t see this happening. The example in 15.8.1 explicitly calls ‘destDirs.mkdirs()’

The following ‘build.gradle’ doesn’t work, for example (with Gradle 1.3).

task hello(type: Exec) {
  outputs.dir "build"
  commandLine "touch", "build/hello"
}

(Luke Daley) #4

This only works when using the annotation based approach to specifying outputs, not when using the programmatic approach. I’ve opened GRADLE-2638 for this.