Introduction to Using GRADLE with ANT

One of the features of Gradle that I like the most is its ability to interoperate with other popular build tools such as ANT and MAVEN. You can integrate and call your existing ant tasks from Gradle as simple as calling ant.<taskname> and Gradle makes this possible  partly by leveraging Groovy’s AntBuilder functionality. This might be required for leveraging an existing investment in Apache Ant in cases such as the overall project has moved to Gradle, but there is not enough time to move all existing ant setup to Gradle or to partly migrate from Ant to Gradle step by step. Gradle also ships with a full copy of Ant, thus making Ant’s default tasks available to every Gradle build. Gradle can even be considered as a Groovy-based Ant.

Gradle can call the Ant echo plug-in as:

task hello << {

String greeting = " Hello World"

ant.echo(message: greeting)

}

This is same as below in ANT:

<echo message="Hello World"/>

We can also write this in ANT as:

<echo> Hello World </echo>

This can be expressed in Gradle as:

ant.echo(greeting)

Note that if you use the –q option while executing the task, you won’t see the ant echo messages. Also, since Ant’s default tasks are available to every Gradle build, there is no need for any additional plugin-in here.

Though both Gradle and Ant has a Directed Acyclic Graph (DAG) of tasks in its execution plan, Gradle has made many improvements and are still looking for improvements and innovations like in memory DAG, parallel execution of discrete paths etc. Ant uses the word task to indicate one of its executable components and these ant tasks can be executed within the context of a target. Gradle defines task to refer to any step of the DAG and a task in Gradle can be most closely compared to a target in Ant. Similar to we execute a target in ant and set dependencies for other targets, in Gradle, we execute a task and set dependencies for other tasks. A task in Gradle can be compared to a plug-in in Gradle and a property in ant can be compared to a variable in Gradle.   

 

Using Custom Ant Tasks in Gradle

You don’t have to do anything extra for core ant tasks such as echo, but you need to import custom ant tasks using the taskdef method on the AntBuilder instance as:

task customTaskCall << {

  //Load the custom task

  ant.taskdef(resource: 'custstyletask.properties') {

    classpath {

      fileset(dir: 'libs’, includes: '*.jar')

    }

  }

}

Here we fetch the parameters needed to load the Ant task from a properties file within one of the jars in the libs directory. You can then use the custom task as: ant.taskname.

The entire loading and execution of the custom Ant task can happen within a single Gradle task. A custom Ant Task can be loaded using a combination of a Gradle configuration, a declared dependency, and Maven Central repository connectivity. See the referenced book for example. In real world Ant configurations, there will be usually several nested elements in the build.xml syntax and Gradle can support them too.

 

Importing Entire Ant Build from Gradle

You can import an entire ANT build file and then treat the Ant build’s contents as a Gradle task. You can import an entire ant project build as:

ant.importBuild 'build.xml'

After importing if you run ‘gradle tasks’, it will show you all tasks runnable from here under headings such as ‘Build Setup tasks’, ‘Help tasks’ and ‘Other Tasks’. ANT targets from the imported build file will be shown under ‘Other Tasks’ along with any custom tasks that we have created. We can now run the ANT target as if it were a Gradle task.

Ant targets can now even declared as a dependency (using dependsOn) of Gradle build tasks.

 

Other important Gradle-ANT integration features

Ant object is a full Groovy AntBuilder instance and hence existing constructs from the Groovy language can also be used here. The more increased understanding of Groovy you have, the more you will benefit to creating effective Gradle build files

We can bring Ant classpaths into the Gradle space and this will help us a lot in a progressive migration from Ant to Gradle. You can establish paths in the Ant realm, and then consume those paths in the Gradle space, including using them as repositories. Paths can also be established in Gradle and then consumed in an Ant target.

We will see these in detail with examples later. You can also refer to the referenced book for examples.

 

Reference

http://ant.apache.org/manual/Tasks/echo.html

Book: Building and Testing with Gradle by Tim Berglund and Matthew McCullough.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)