How does my task 'dependOn' the build folder already existing?


(Chad Davis) #1

My task needs the build folder to already exist. I’m just dropping something into it. I could “dependsOn” javaCompile just to make the build folder be there, but that seems wrong. Should I just test for it’s existence? Should I depend on someother java plugin task that I don’t know about?


(Peter Niederwieser) #2

A task can either create missing target directories itself, or declare the file/files/directory/directories it writes to as task outputs, in which case missing target directory(s) will be created automatically. For details on how to declare task outputs, see Declaring a task’s inputs and outputs in the Gradle User Guide. If the task is implemented as a class, an alternative (and preferable) way to declare its outputs is to use annotations such as ‘@OutputFile’, ‘@OutputFiles’, ‘@OutputDirectory’, and ‘@OutputDirectories’ on the corresponding properties/getter methods of the class.


(Chad Davis) #3

Hmm. It doesn’t work though :slight_smile: I’m working with the User Guide example you gave me. Here’s the listing, for convenient viewing.

Note, the doLast closure action creates the output directories. If I remove that line, it fails because it can’t find the output directory. Are you suggesting that it should automatically create all dirs for output.

task transform {

ext.srcFile = file(‘mountains.xml’)

ext.destDir = new File(buildDir, ‘generated’)

inputs.file srcFile

outputs.dir destDir

doLast {

println “Transforming source file.”

destDir.mkdirs()

def mountains = new XmlParser().parse(srcFile)

mountains.mountain.each { mountain ->

def name = mountain.name[0].text()

def height = mountain.height[0].text()

def destFile = new File(destDir, “${name}.txt”)

destFile.text = “$name -> ${height}\n”

}

} }


(Chad Davis) #4

Perhaps it only works using the annotations?


(Peter Niederwieser) #5

API and annotation should behave the same.