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?
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.
Hmm. It doesn’t work though 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”
}
} }
Perhaps it only works using the annotations?
API and annotation should behave the same.