Creating a task in the project source directory
In the previous section we have defined and used our own enhanced task in the same build file. Next, we are going to extract the class definition from the build file and put it in a separate file. We are going to place the file in the buildSrc
project source directory.
Let's move our InfoTask
to the buildSrc
directory of our project. We first create the buildSrc/src/main/groovy/sample
directory. We create a file named InfoTask.groovy
in this directory, with the following code:
package sample import org.gradle.api.* import org.gradle.api.tasks.* class InfoTask extends DefaultTask { String prefix = 'Current Gradle version' @TaskAction def info() { println "$prefix: $project.gradle.gradleVersion" } }
Notice that we must add import statements for the classes of the Gradle API. These imports are implicitly added to a build script by Gradle, but if we define the task outside the build script, we must add the import statements...