I’m working on a custom task that puts a new artifact directly in the build folder (more specifically, wherever project.buildDir is pointing). The task is not creating the build folder, because it seems like that should not be the responsibility of the task. The new task works fine when the build task is among the tasks that are specified on the Gradle command line, but it fails otherwise, for example, when we execute “gradle test release”. How can the custom task ensure that the build folder has been created?
It is in fact the responsibility of the task. Only tasks should create output, therefore the task is responsible for creating the output directory, should it not exist. ‘Project.getBuildDir()’ is more convention than anything, since a task could just as well place its output outside of that directory.
The simplest way to accomplish this is by leveraging the ‘@OutputFile’ or ‘@OutputDirectory’. Using either annotation on your task properties will automatically create the directory (or parent directory) as required.