Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations

In a previous post we learned how we can use the inputs and outputs properties to set properties or files that need to be checked to see if a task is up to date. In this post we learn how a custom task class can use annotations to set input properties, file or files and output files or dir.

For input we can use @Input, @InputFile, @InputFiles or @InputDirectory annotations. Gradle uses the properties with annotations for checking if a task is up to date. Output file or directory can be marked with @OutputFile and @OutputDirectory.

view sourceprint?

00.task generateVersionFile(type: Generate) {

01.version = ‘2.0‘

02.outputFile = file("$project.buildDir/version.txt")

03.}

04.

05.task showContents << {

06.println generateVersionFile.outputFile.text

07.}

08.showContents.dependsOn generateVersionFile

09.

10.class Generate extends DefaultTask {

11.@Input

12.String version

13.

14.@OutputFile

15.File outputFile

16.

17.@TaskAction

18.void generate() {

19.def file = getOutputFile()

20.if (!file.isFile()) {

21.file.parentFile.mkdirs()

22.file.createNewFile()

23.}

24.file.write "Version: ${getVersion()}"

25.}

26.}

We can run our task and get the following output:

$ gradle showContents

:generateVersionFile

:showContents

Version: 2.0

BUILD SUCCESSFUL

And if we run it again we see the task is now up to date:

$ gradle showContents

:generateVersionFile UP-TO-DATE

:showContents

Version: 2.0

BUILD SUCCESSFUL

We can change the version numer in our build script to 2.1 and see the output:

$ gradle showContents

:generateVersionFile

:showContents

Version: 2.1

BUILD SUCCESSFUL

时间: 2024-10-17 14:13:40

Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations的相关文章

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T

Gradle Goodness: Run a Build Script With a Different Name

Normally Gradle looks for a build script file with the name build.gradle in the current directory to execute a build. But we can easily use a different name or directory for the build file. We only have to use the -b or --build-file command-line opti

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Gradle Goodness: Unpacking an Archive

To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to create a new archive. But there is no UnZip or UnTar to unpack an archive in Gradle. To unpack an archive we must use the Copy task and copy the conten

Gradle Goodness: Continue Build Even with Failed Tasks

If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the comman

Gradle Goodness: Changing Name of Default Build File

Gradle uses the name build.gradle as the default name for a build file. If we write our build code in a file build.gradle then we don't have to specify the build filename when we run tasks. We can create build files with a different name other than b

Gradle Goodness: Adding Tasks to a Predefined Group

In Gradle we can group related tasks using the group property of a task. We provide the name of our group and if we look at the output of the tasks task we can see our tasks grouped in section with the given name. In the next sample we create a new t

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method