Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Gradle calls Ant from Gradle


May 25, 2021 Gradle


Table of contents


Call Ant from Gradle

Gradle provides great integration into Ant You can use separate Ant tasks or the entire Ant build in your Gradle build. I n fact, you'll find that using Ant tasks in Gradle is easier and more powerful than using Ant's XML format. You can even just use Gradle as a powerful tool for Ant task scripting.

Ant can be divided into two layers. T he first layer is the language of Ant. I t provides syntax for building .xml, processing targets, special construction methods such as macros, and more. I n other words, all but Ant tasks and types are available. G radle understands this language and allows you to import your Ant build .xml to the Gradle project. You can then use target in your Ant build as if they were Gradle tasks.

The second layer of Ant is its rich Ant tasks and types, such as javac, copy, or jar. This layer of Gradle is integrated with Groovy and the incredible AntBuilder alone.

Finally, because the build script is a Groovy script, you can always execute the Ant build as an external process. Y our build script might contain a statement like this: "ant clean compile".execute(). [8]

You can use Gradle's Ant integration as a path to migrate your build from Ant to Gradle. F or example, you can start by importing your existing Ant build. Y ou can then move your dependency claims from the Ant script to your build file. F inally, you can move the entire task to your build files or replace them with some Gradle plug-ins. This process can be done a little over time, and your Gradle build can be used throughout the process.

Use Ant tasks and types in your build

In the build script, Gradle provides a property called ant. I t points to an AntBuilder instance. A ntBuilder is used to access Ant tasks, types, and properties from your build script. There is a very simple .xml from Ant's build format to Groovy, as explained below.

You can perform an Ant task by calling a method on the AntBuilder instance. Y ou can use the task name as a method name. F or example, you can perform Ant's echo task by calling the ant.echo() method. T he properties of the Ant task are passed to the method as Map parameters. T he following is an example of performing an echo task. N ote that we can also use a mix of Groovy code and Ant task tags. It's going to be very powerful.

Use Ant tasks

build.gradle

task hello << {
    String greeting = 'hello from Ant'
    ant.echo(message: greeting)
}  

The output of the gradle hello

> gradle hello
:hello
[ant:echo] hello from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

You can pass a nested text to an Ant task by calling it as an argument to a task method. In this example, we'll pass the message as nested text to the echo task:

Incoming nested text to the Ant task

build.gradle

task hello << {
    ant.echo('hello from Ant')
}  

The output of the gradle hello

> gradle hello
:hello
[ant:echo] hello from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

You can pass nested elements to an Ant task in a closure. Nested elements are defined in the same way as tasks by calling methods with the same name as the elements we want to define.

A nested element is passed in to the Ant task

build.gradle

task zip << {
    ant.zip(destfile: 'archive.zip') {
        fileset(dir: 'src') {
            include(name: '**.xml')
            exclude(name: '**.java')
        }
    }
}    

You can access the Ant type in the same way as the access task, using the type name as the method name. M ethod calls return the Ant data type, which can then be used directly in the build script. In the following example, we create an Ant path object and then loop through its contents.

Use the Ant type

build.gradle

task list << {
    def path = ant.path {
        fileset(dir: 'libs', includes: '*.jar')
    }
    path.list().each {
        println it
    }
}  

Use custom Ant tasks in your build

To make custom tasks available in your build, you can use the Ant task taskdef (usually easier) or typedef, just as you would .xml in a build file. You can then reference custom Ant tasks as if they were built-in Ant tasks.

Use custom Ant tasks

build.gradle

task check << {
    ant.taskdef(resource: 'checkstyletask.properties') {
        classpath {
            fileset(dir: 'libs', includes: '*.jar')
        }
    }
    ant.checkstyle(config: 'checkstyle.xml') {
        fileset(dir: 'src')
    }
}  

You can use Gradle's dependency management combination class paths for custom tasks. To do this, you need to define a custom configuration of the class path, and then add some dependencies to the configuration.

Declares the class path used to customize the Ant task

build.gradle

configurations {
    pmd
}
dependencies {
    pmd group: 'pmd', name: 'pmd', version: '4.2.5'
}  

To use the class path configuration, use the asPath property in the custom configuration.

Use custom Ant tasks and dependency management at the same time

build.gradle

task check << {
    ant.taskdef(name: 'pmd', classname: 'net.sourceforge.pmd.ant.PMDTask', classpath: configurations.pmd.asPath)
    ant.pmd(shortFilenames: 'true', failonruleviolation: 'true', rulesetfiles: file('pmd-rules.xml').toURI().toString()) {
        formatter(type: 'text', toConsole: 'true')
        fileset(dir: 'src')
    }
}  

Import the Ant build

You can use the ant.importBuild() method to import an Ant build into a Gradle project. W hen you import an Ant build, each Ant target is treated as a Gradle task. This means that you can manipulate and execute Ant targets in a way that is fully camera-like with the Gradle task.

Import the Ant build

build.gradle

ant.importBuild 'build.xml'
build.xml
<project>
    <target name="hello">
        <echo>Hello, from Ant</echo>
    </target>
</project>  

The output of the gradle hello

> gradle hello
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

You can add a task that depends on the Ant target:

Tasks that depend on the Ant target

build.gradle

ant.importBuild 'build.xml'
task intro(dependsOn: hello) << {
    println 'Hello, from Gradle'
}  

The output of the gradle intro

> gradle intro
:hello
[ant:echo] Hello, from Ant
:intro
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs  

Alternatively, you can add behavior to the Ant target:

Add behavior to the Ant target

build.gradle

ant.importBuild 'build.xml'
hello << {
    println 'Hello, from Gradle'
}  

The output of the gradle hello

> gradle hello
:hello
[ant:echo] Hello, from Ant
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs  

It can also be used for an Ant target that relies on the Gradle task:

Ant targets that depend on the Gradle task

build.gradle

ant.importBuild 'build.xml'
task intro << {
    println 'Hello, from Gradle'
}
build.xml
<project>
    <target name="hello" depends="intro">
        <echo>Hello, from Ant</echo>
    </target>
</project>  

The output of the gradle hello

> gradle hello
:intro
Hello, from Gradle
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

Ant properties and references

There are several ways to set the Ant property so that it is used by the Ant task. Y ou can set properties directly on the AntBuilder instance. T he Ant property is also available from a Map that you can modify. Y ou can also use the Ant Property task. Here are some examples of how to do this.

Ant property settings

build.gradle

ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name: 'buildDir', location: buildDir)
build.xml
<echo>buildDir = ${buildDir}</echo>  

Many Ant tasks set some properties when they are executed. T here are several ways to get these property values. Y ou can get properties directly from the AntBuilder instance. T he Ant property can also be used as a Map. Here are some examples.

Gets the Ant property

build.xml

<property name="antProp" value="a property defined in an Ant build"/>
build.gradle
println ant.antProp
println ant.properties.antProp
println ant.properties['antProp']  

There are several ways to set an Ant reference:

Ant reference settings

build.gradle

ant.path(id: 'classpath', location: 'libs')
ant.references.classpath = ant.path(location: 'libs')
ant.references['classpath'] = ant.path(location: 'libs')
build.xml
<path refid="classpath"/>  

There are several ways to get an Ant reference:

Get the Ant reference

build.xml

<path id="antPath" location="libs"/>
build.gradle
println ant.references.antPath
println ant.references['antPath']