Home » Java » Enterprise Java » Differentiating Ant Target-Based Gradle Tasks

About Dustin Marx

Differentiating Ant Target-Based Gradle Tasks

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 (Version 1)

// 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.

ant-targets-gradle-tasks-all-same

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.

ant-targets-gradle-tasks-vary

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).
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


× 4 = sixteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.