Engineering Full Stack Apps with Java and JavaScript
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.
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.
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.
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.
http://ant.apache.org/manual/Tasks/echo.html
Book: Building and Testing with Gradle by Tim Berglund and Matthew McCullough.