// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[ant]]
= Using Ant from Gradle

Gradle provides excellent integration with Ant. You can use individual Ant tasks or entire Ant builds in your Gradle builds. In fact, you will find that it's far easier and more powerful using Ant tasks in a Gradle build script, than it is to use Ant's XML format. You could even use Gradle simply as a powerful Ant task scripting tool.

Ant can be divided into two layers. The first layer is the Ant language. It provides the syntax for the `build.xml` file, the handling of the targets, special constructs like macrodefs, and so on. In other words, everything except the Ant tasks and types. Gradle understands this language, and allows you to import your Ant `build.xml` directly into a Gradle project. You can then use the targets of your Ant build as if they were Gradle tasks.

The second layer of Ant is its wealth of Ant tasks and types, like `javac`, `copy` or `jar`. For this layer Gradle provides integration simply by relying on Groovy, and the fantastic `AntBuilder`.

Finally, since build scripts are Groovy scripts, you can always execute an Ant build as an external process. Your build script may contain statements like: `"ant clean compile".execute()`.footnote:[In Groovy you can execute Strings. To learn more about executing external processes with Groovy have a look in 'Groovy in Action' 9.3.2 or at the Groovy wiki]

You can use Gradle's Ant integration as a path for migrating your build from Ant to Gradle. For example, you could start by importing your existing Ant build. Then you could move your dependency declarations from the Ant script to your build file. Finally, you could move your tasks across to your build file, or replace them with some of Gradle's plugins. This process can be done in parts over time, and you can have a working Gradle build during the entire process.


[[sec:using_ant_tasks]]
== Using Ant tasks and types in your build

In your build script, a property called `ant` is provided by Gradle. This is a reference to an link:{javadocPath}/org/gradle/api/AntBuilder.html[AntBuilder] instance. This `AntBuilder` is used to access Ant tasks, types and properties from your build script. There is a very simple mapping from Ant's `build.xml` format to Groovy, which is explained below.

You execute an Ant task by calling a method on the `AntBuilder` instance. You use the task name as the method name. For example, you execute the Ant `echo` task by calling the `ant.echo()` method. The attributes of the Ant task are passed as Map parameters to the method. Below is an example of the `echo` task. Notice that we can also mix Groovy code and the Ant task markup. This can be extremely powerful.

.Using an Ant task
====
include::sample[dir="snippets/userguide/ant/useAntTask/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/useAntTask/kotlin",files="build.gradle.kts"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/useAntTask/useAntTask.out[]
----

You pass nested text to an Ant task by passing it as a parameter of the task method call. In this example, we pass the message for the `echo` task as nested text:

.Passing nested text to an Ant task
====
include::sample[dir="snippets/userguide/ant/taskWithNestedText/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/taskWithNestedText/kotlin",files="build.gradle.kts"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/taskWithNestedText/taskWithNestedText.out[]
----

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

.Passing nested elements to an Ant task
====
include::sample[dir="snippets/userguide/ant/taskWithNestedElements/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/taskWithNestedElements/kotlin",files="build.gradle.kts"]
====

You can access Ant types in the same way that you access tasks, using the name of the type as the method name. The method call returns the Ant data type, which you can then use directly in your build script. In the following example, we create an Ant `path` object, then iterate over the contents of it.

.Using an Ant type
====
include::sample[dir="snippets/userguide/ant/useAntType/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/useAntType/kotlin",files="build.gradle.kts"]
====

More information about `AntBuilder` can be found in 'Groovy in Action' 8.4 or at the https://groovy-lang.org/scripting-ant.html[Groovy Wiki].

[[sec:using_custom_ant_tasks]]
=== Using custom Ant tasks in your build

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

.Using a custom Ant task
====
include::sample[dir="snippets/userguide/ant/useExternalAntTask/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/useExternalAntTask/kotlin",files="build.gradle.kts"]
====

You can use Gradle's dependency management to assemble the classpath to use for the custom tasks. To do this, you need to define a custom configuration for the classpath, then add some dependencies to the configuration. This is described in more detail in link:declaring_dependencies.html[Declaring Dependencies].

.Declaring the classpath for a custom Ant task
====
include::sample[dir="snippets/userguide/ant/useExternalAntTaskWithConfig/groovy",files="build.gradle[tags=define-classpath]"]
include::sample[dir="snippets/userguide/ant/useExternalAntTaskWithConfig/kotlin",files="build.gradle.kts[tags=define-classpath]"]
====

To use the classpath configuration, use the `asPath` property of the custom configuration.

.Using a custom Ant task and dependency management together
====
include::sample[dir="snippets/userguide/ant/useExternalAntTaskWithConfig/groovy",files="build.gradle[tags=use-classpath]"]
include::sample[dir="snippets/userguide/ant/useExternalAntTaskWithConfig/kotlin",files="build.gradle.kts[tags=use-classpath]"]
====

[[sec:import_ant_build]]
== Importing an Ant build

You can use the `ant.importBuild()` method to import an Ant build into your Gradle project. When you import an Ant build, each Ant target is treated as a Gradle task. This means you can manipulate and execute the Ant targets in exactly the same way as Gradle tasks.

.Importing an Ant build
====
include::sample[dir="snippets/userguide/ant/hello/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/hello/kotlin",files="build.gradle.kts"]
====

====
include::sample[dir="snippets/userguide/ant/hello/groovy",files="build.xml"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/hello/antHello.out[]
----

You can add a task which depends on an Ant target:

.Task that depends on Ant target
====
include::sample[dir="snippets/userguide/ant/dependsOnAntTarget/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/dependsOnAntTarget/kotlin",files="build.gradle.kts"]
====

.Output of `gradle intro`
----
> gradle intro
include::{samplesPath}/userguide/ant/dependsOnAntTarget/dependsOnAntTarget.out[]
----

Or, you can add behaviour to an Ant target:

.Adding behaviour to an Ant target
====
include::sample[dir="snippets/userguide/ant/addBehaviourToAntTarget/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/addBehaviourToAntTarget/kotlin",files="build.gradle.kts"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/addBehaviourToAntTarget/addBehaviourToAntTarget.out[]
----

It is also possible for an Ant target to depend on a Gradle task:

.Ant target that depends on Gradle task
====
include::sample[dir="snippets/userguide/ant/dependsOnTask/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/dependsOnTask/kotlin",files="build.gradle.kts"]
====

====
include::sample[dir="snippets/userguide/ant/dependsOnTask/groovy",files="build.xml"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/dependsOnTask/dependsOnTask.out[]
----

Sometimes it may be necessary to “rename” the task generated for an Ant target to avoid a naming collision with existing Gradle tasks. To do this, use the link:{javadocPath}/org/gradle/api/AntBuilder.html#importBuild-java.lang.Object-org.gradle.api.Transformer-[AntBuilder.importBuild(java.lang.Object, org.gradle.api.Transformer)] method.

.Renaming imported Ant targets
====
include::sample[dir="snippets/userguide/ant/renameTask/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/renameTask/kotlin",files="build.gradle.kts"]
====

====
include::sample[dir="snippets/userguide/ant/renameTask/groovy",files="build.xml"]
====

.Output of `gradle a-hello`
----
> gradle a-hello
include::{samplesPath}/userguide/ant/renameTask/renameAntDelegate.out[]
----

Note that while the second argument to this method should be a link:{javadocPath}/org/gradle/api/Transformer.html[Transformer], when programming in Groovy we can simply use a closure instead of an anonymous inner class (or similar) due to http://mrhaki.blogspot.ie/2013/11/groovy-goodness-implicit-closure.html[Groovy's support for automatically coercing closures to single-abstract-method types].

[[sec:ant_properties]]
== Ant properties and references

There are several ways to set an Ant property, so that the property can be used by Ant tasks. You can set the property directly on the `AntBuilder` instance. The Ant properties are also available as a Map which you can change. You can also use the Ant `property` task. Below are some examples of how to do this.

.Setting an Ant property
====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.gradle[tags=set-property]"]
include::sample[dir="snippets/userguide/ant/properties/kotlin",files="build.gradle.kts[tags=set-property]"]
====

Many Ant tasks set properties when they execute. There are several ways to get the value of these properties. You can get the property directly from the `AntBuilder` instance. The Ant properties are also available as a Map. Below are some examples.

.Getting an Ant property
====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.xml[tags=getproperty]"]
====

====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.gradle[tags=get-property]"]
include::sample[dir="snippets/userguide/ant/properties/kotlin",files="build.gradle.kts[tags=get-property]"]
====

There are several ways to set an Ant reference:

.Setting an Ant reference
====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.gradle[tags=set-reference]"]
include::sample[dir="snippets/userguide/ant/properties/kotlin",files="build.gradle.kts[tags=set-reference]"]
====

====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.xml[tags=setreference]"]
====

There are several ways to get an Ant reference:

.Getting an Ant reference
====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.xml[tags=getreference]"]
====

====
include::sample[dir="snippets/userguide/ant/properties/groovy",files="build.gradle[tags=get-reference]"]
include::sample[dir="snippets/userguide/ant/properties/kotlin",files="build.gradle.kts[tags=get-reference]"]
====

[[sec:ant_logging]]
== Ant logging

Gradle maps Ant message priorities to Gradle log levels so that messages logged from Ant appear in the Gradle output. By default, these are mapped as follows:

.Ant message priority mapping
[cols="a,a", options="header"]
|===
| Ant Message Priority
| Gradle Log Level

| _VERBOSE_
| `DEBUG`

| _DEBUG_
| `DEBUG`

| _INFO_
| `INFO`

| _WARN_
| `WARN`

| _ERROR_
| `ERROR`
|===


[[sec:fine_tuning_ant_logging]]
=== Fine tuning Ant logging

The default mapping of Ant message priority to Gradle log level can sometimes be problematic. For example, there is no message priority that maps directly to the `LIFECYCLE` log level, which is the default for Gradle. Many Ant tasks log messages at the _INFO_ priority, which means to expose those messages from Gradle, a build would have to be run with the log level set to `INFO`, potentially logging much more output than is desired.

Conversely, if an Ant task logs messages at too high of a level, to suppress those messages would require the build to be run at a higher log level, such as `QUIET`. However, this could result in other, desirable output being suppressed.

To help with this, Gradle allows the user to fine tune the Ant logging and control the mapping of message priority to Gradle log level. This is done by setting the priority that should map to the default Gradle `LIFECYCLE` log level using the link:{javadocPath}/org/gradle/api/AntBuilder.html#setLifecycleLogLevel-java.lang.String-[AntBuilder.setLifecycleLogLevel(java.lang.String)] method. When this value is set, any Ant message logged at the configured priority or above will be logged at least at `LIFECYCLE`. Any Ant message logged below this priority will be logged at most at `INFO`.

For example, the following changes the mapping such that Ant _INFO_ priority messages are exposed at the `LIFECYCLE` log level.

.Fine tuning Ant logging
====
include::sample[dir="snippets/userguide/ant/antLogging/groovy",files="build.gradle"]
include::sample[dir="snippets/userguide/ant/antLogging/kotlin",files="build.gradle.kts"]
====

.Output of `gradle hello`
----
> gradle hello
include::{samplesPath}/userguide/ant/antLogging/antLogging.out[]
----

On the other hand, if the `lifecycleLogLevel` was set to _ERROR_, Ant messages logged at the _WARN_ priority would no longer be logged at the `WARN` log level. They would now be logged at the `INFO` level and would be suppressed by default.

[[sec:ant_api]]
== API

The Ant integration is provided by link:{javadocPath}/org/gradle/api/AntBuilder.html[AntBuilder].
