In my blog post Evolving Gradle Build from Ant Build: Importing Ant Build File, I demonstrated using Gradle's built-in AntBuilder-based Ant support to import Ant targets in a Gradle build. These Ant targets can then be accessed as Gradle tasks and appear the same as tasks introduced directly by the Gradle build. In this post, I look at using Groovy to differentiate between Gradle tasks based on imported Ant targets and Gradle-defined tasks.
The Ant build file imported by the Gradle builds in my examples of this post was introduced in my previous post. That Ant build file included the targets "-init", "clean", "compile", "jar", "javadoc", "all", and "output". The Gradle build script file that follows imports that build.xml
and its Ant targets.
// build-ant-targets-and-gradle-tasks.gradle // // Gradle build script demonstrating Gradle Tasks associated with this project // that are not provided by an imported Ant build file. // ant is a DefaultAntBuilder instance ant.importBuild 'build.xml' def antTargetsNames = ant.references.get("ant.targets").collect{ it.name } println "\nAnt Targets: ${antTargetsNames}\n" def taskNames = rootProject.tasks.collect{ it.name } println "\nGradle Task Names: ${taskNames}\n" def tasksThatAreNotAntTargets = taskNames - antTargetsNames println "\nGradle Tasks that are NOT Ant Targets: ${tasksThatAreNotAntTargets}\n"
The implicitly available "ant" variable (default AntBuilder) is used first to get all Ant-provided targets via the call ant.references.get("ant.targets")
. Groovy's handy Collection.collect(Closure) method is invoked upon that collection to return a collection of the "names" of the Ant targets.
The implicitly available "rootProject" can also be used to get the tasks at the root project level using rootProject.tasks
. The same Groovy Collection.collect(Closure)
method is used on this collection to get the names of the Gradle tasks. Finally, Groovy's subtraction operator is used to easily determine which Gradle Tasks are not Ant Targets. When run as shown above all the Gradle Tasks are Ant Targets and so the subtraction operator returns nothing. This is shown in the next screen snapshot.
To make the example more interesting, I add a couple of Gradle-introduced Tasks to the Gradle build file shown above. The new version with two new Gradle-introduced Tasks is shown next.
build-ant-targets-and-gradle-tasks.gradle (Version 2)// build-ant-targets-and-gradle-tasks.gradle // // Gradle build script demonstrating Gradle Tasks associated with this project // that are not provided by an imported Ant build file. // ant is a DefaultAntBuilder instance ant.importBuild 'build.xml' task(helloWorld) << { println "Hello, World!" } task(currentDateTime) << { println new Date() } def antTargetsNames = ant.references.get("ant.targets").collect{ it.name } println "\nAnt Targets: ${antTargetsNames}\n" def taskNames = rootProject.tasks.collect{ it.name } println "\nGradle Task Names: ${taskNames}\n" def tasksThatAreNotAntTargets = taskNames - antTargetsNames println "\nGradle Tasks that are NOT Ant Targets: ${tasksThatAreNotAntTargets}\n"
This revised version of the Gradle build script introduces two Tasks of its own ("helloWorld" and "currentDateTime"). The output from running this script includes these two new Gradle tasks as Gradle tasks that are not Ant-introduced, Target-based Gradle tasks. This output is shown next.
The examples in this post provide additional examples of the advantages of being able to use Groovy code to better understand Gradle builds. It is straightforward to access the default Ant Builder instance ("ant") and the "rootProject " to get the names of all Ant-based Gradle tasks as well as all tasks (Ant-based or Gradle-introduced).
No comments:
Post a Comment