/usr/share/doc/gradle/userguide/ant.html is in gradle-doc 3.4.1-7ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | <html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Using Ant from Gradle - Gradle User Guide Version 3.4.1</title><link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="base.css"><link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="docs.css"><link xmlns:xslthl="http://xslthl.sf.net" type="text/css" rel="stylesheet" href="userguide.css"><meta content="DocBook XSL Stylesheets V1.79.1" name="generator"><link rel="home" href="userguide.html" title="Gradle User Guide Version 3.4.1"><link rel="up" href="pt03.html" title="Part III. Writing Gradle build scripts"><link rel="prev" href="working_with_files.html" title="Working With Files - Gradle User Guide Version 3.4.1"><link rel="next" href="build_lifecycle.html" title="The Build Lifecycle - Gradle User Guide Version 3.4.1"></head><body><div class="navheader"><div><div class="navbar"><a xmlns:xslthl="http://xslthl.sf.net" href="working_with_files.html" title="Working With Files - Gradle User Guide Version 3.4.1">Previous</a><span>|</span><a xmlns:xslthl="http://xslthl.sf.net" href="userguide.html" title="Gradle User Guide Version 3.4.1">Contents</a><span>|</span><a xmlns:xslthl="http://xslthl.sf.net" href="build_lifecycle.html" title="The Build Lifecycle - Gradle User Guide Version 3.4.1">Next</a></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 xmlns:xslthl="http://xslthl.sf.net"><a name="ant"></a>Chapter 21. Using Ant from Gradle</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="ant.html#sec:using_ant_tasks">21.1. Using Ant tasks and types in your build</a></span></dt><dt><span class="section"><a href="ant.html#sec:import_ant_build">21.2. Importing an Ant build</a></span></dt><dt><span class="section"><a href="ant.html#sec:ant_properties">21.3. Ant properties and references</a></span></dt><dt><span class="section"><a href="ant.html#sec:ant_logging">21.4. Ant logging</a></span></dt><dt><span class="section"><a href="ant.html#sec:ant_api">21.5. API</a></span></dt></dl></div><p>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.
</p><p>Ant can be divided into two layers. The first layer is the Ant language. It provides the syntax for the
<code class="filename">build.xml</code> 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 <code class="filename">build.xml</code> directly into a Gradle project. You can then use the targets of
your Ant build as if they were Gradle tasks.
</p><p>The second layer of Ant is its wealth of Ant tasks and types, like <code class="literal">javac</code>,
<code class="literal">copy</code> or <code class="literal">jar</code>. For this layer Gradle provides integration
simply by relying on Groovy, and the fantastic <code class="literal">AntBuilder</code>.
</p><p>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:<code class="literal">"ant clean compile".execute()</code>.
<a href="#ftn.N1205F" class="footnote" name="N1205F"><sup class="footnote">[9]</sup></a>
</p><p>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.
</p><div class="section"><div class="titlepage"><div><div><h2 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:using_ant_tasks" class="section-anchor" href="#sec:using_ant_tasks"></a>21.1. Using Ant tasks and types in your build</h2></div></div></div><p>In your build script, a property called <code class="literal">ant</code> is provided by Gradle. This is a reference
to an <a class="ulink" href="../javadoc/org/gradle/api/AntBuilder.html" target="_top"><code class="classname">AntBuilder</code></a> instance. This <code class="literal">AntBuilder</code> is used to
access Ant tasks, types and properties from your build script. There is a very simple mapping from Ant's
<code class="filename">build.xml</code> format to Groovy, which is explained below.
</p><p>You execute an Ant task by calling a method on the <code class="literal">AntBuilder</code> instance. You use the task
name as the method name. For example, you execute the Ant <code class="literal">echo</code> task by calling the
<code class="literal">ant.echo()</code> method. The attributes of the Ant task are passed as Map parameters to the
method. Below is an example of the <code class="literal">echo</code> task. Notice that we can also mix
Groovy code and the Ant task markup. This can be extremely powerful.</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="useAntTask"></a><p class="title"><b>Example 21.1. Using an Ant task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task hello {
doLast {
String greeting = <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'hello from Ant'</span>
ant.echo(message: greeting)
}
}
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:hello
[ant:echo] hello from Ant
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>
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 <code class="literal">echo</code> task as nested text:
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="taskWithNestedText"></a><p class="title"><b>Example 21.2. Passing nested text to an Ant task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task hello {
doLast {
ant.echo(<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'hello from Ant'</span>)
}
}
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:hello
[ant:echo] hello from Ant
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>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.</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="taskWithNestedElements"></a><p class="title"><b>Example 21.3. Passing nested elements to an Ant task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task zip {
doLast {
ant.zip(destfile: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'archive.zip'</span>) {
fileset(dir: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'src'</span>) {
include(name: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'**.xml'</span>)
exclude(name: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'**.java'</span>)
}
}
}
}
</pre></div></div><br class="example-break"><p>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 <code class="literal">path</code> object, then iterate over the contents of it.</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="useAntType"></a><p class="title"><b>Example 21.4. Using an Ant type</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task list {
doLast {
def path = ant.path {
fileset(dir: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'libs'</span>, includes: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'*.jar'</span>)
}
path.list().each {
println it
}
}
}
</pre></div></div><br class="example-break"><p>More information about <code class="literal">AntBuilder</code> can be found in 'Groovy in Action' 8.4 or at the
<a class="ulink" href="http://groovy-lang.org/scripting-ant.html" target="_top">Groovy Wiki</a>
</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:using_custom_ant_tasks" class="section-anchor" href="#sec:using_custom_ant_tasks"></a>21.1.1. Using custom Ant tasks in your build</h3></div></div></div><p>To make custom tasks available in your build,
you can use the <code class="literal">taskdef</code> (usually easier) or <code class="literal">typedef</code> Ant task,
just as you would in a <code class="literal">build.xml</code> file. You can then refer to the custom Ant task as you
would a built-in Ant task.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="useExternalAntTask"></a><p class="title"><b>Example 21.5. Using a custom Ant task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task check {
doLast {
ant.taskdef(resource: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'checkstyletask.properties'</span>) {
classpath {
fileset(dir: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'libs'</span>, includes: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'*.jar'</span>)
}
}
ant.checkstyle(config: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'checkstyle.xml'</span>) {
fileset(dir: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'src'</span>)
}
}
}
</pre></div></div><br class="example-break"><p>
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 <a class="xref" href="dependency_management.html#sec:how_to_declare_your_dependencies">Section 25.4, “How to declare your dependencies”</a>.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="useExternalAntTaskWithConfig"></a><p class="title"><b>Example 21.6. Declaring the classpath for a custom Ant task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">configurations {
pmd
}
dependencies {
pmd group: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'pmd'</span>, name: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'pmd'</span>, version: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'4.2.5'</span>
}
</pre></div></div><br class="example-break"><p>To use the classpath configuration, use the <code class="literal">asPath</code> property of the custom configuration.</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="useExternalAntTaskWithConfig"></a><p class="title"><b>Example 21.7. Using a custom Ant task and dependency management together</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">task check {
doLast {
ant.taskdef(name: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'pmd'</span>,
classname: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'net.sourceforge.pmd.ant.PMDTask'</span>,
classpath: configurations.pmd.asPath)
ant.pmd(shortFilenames: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'true'</span>,
failonruleviolation: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'true'</span>,
rulesetfiles: file(<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'pmd-rules.xml'</span>).toURI().toString()) {
formatter(type: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'text'</span>, toConsole: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'true'</span>)
fileset(dir: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'src'</span>)
}
}
}
</pre></div></div><br class="example-break"></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:import_ant_build" class="section-anchor" href="#sec:import_ant_build"></a>21.2. Importing an Ant build</h2></div></div></div><p>You can use the <code class="literal">ant.importBuild()</code> 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.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antHello"></a><p class="title"><b>Example 21.8. Importing an Ant build</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.importBuild <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'build.xml'</span>
</pre><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><project></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><target</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">name</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"hello"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><echo></span>Hello, from Ant<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></echo></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></target></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></project></span>
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>You can add a task which depends on an Ant target:
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="dependsOnAntTarget"></a><p class="title"><b>Example 21.9. Task that depends on Ant target</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.importBuild <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'build.xml'</span>
task intro(dependsOn: hello) {
doLast {
println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello, from Gradle'</span>
}
}
</pre><p>Output of <strong class="userinput"><code>gradle intro</code></strong></p><pre class="screen">> gradle intro
:hello
[ant:echo] Hello, from Ant
:intro
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>Or, you can add behaviour to an Ant target:</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="addBehaviourToAntTarget"></a><p class="title"><b>Example 21.10. Adding behaviour to an Ant target</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.importBuild <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'build.xml'</span>
hello {
doLast {
println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello, from Gradle'</span>
}
}
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:hello
[ant:echo] Hello, from Ant
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>It is also possible for an Ant target to depend on a Gradle task:</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="dependsOnTask"></a><p class="title"><b>Example 21.11. Ant target that depends on Gradle task</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.importBuild <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'build.xml'</span>
task intro {
doLast {
println <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'Hello, from Gradle'</span>
}
}
</pre><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><project></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><target</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">name</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"hello"</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">depends</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"intro"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><echo></span>Hello, from Ant<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></echo></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></target></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></project></span>
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:intro
Hello, from Gradle
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>
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 <a class="ulink" href="../javadoc/org/gradle/api/AntBuilder.html#importBuild(java.lang.Object, org.gradle.api.Transformer)" target="_top"><code class="classname">AntBuilder.importBuild(java.lang.Object, org.gradle.api.Transformer)</code></a> method.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="renameAntDelegate"></a><p class="title"><b>Example 21.12. Renaming imported Ant targets</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.importBuild(<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'build.xml'</span>) { antTargetName ->
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'a-'</span> + antTargetName
}
</pre><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><project></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><target</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">name</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"hello"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><echo></span>Hello, from Ant<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></echo></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></target></span>
<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></project></span>
</pre><p>Output of <strong class="userinput"><code>gradle a-hello</code></strong></p><pre class="screen">> gradle a-hello
:a-hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>
Note that while the second argument to this method should be a <a class="ulink" href="../javadoc/org/gradle/api/Transformer.html" target="_top"><code class="classname">Transformer</code></a>, when programming in Groovy we can simply
use a closure instead of an anonymous inner class (or similar) due to <a class="ulink" href="http://mrhaki.blogspot.ie/2013/11/groovy-goodness-implicit-closure.html" target="_top">Groovy's support for automatically coercing closures to single-abstract-method types</a>.
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:ant_properties" class="section-anchor" href="#sec:ant_properties"></a>21.3. Ant properties and references</h2></div></div></div><p>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 <code class="literal">AntBuilder</code> instance. The Ant properties are also
available as a Map which you can change. You can also use the Ant <code class="literal">property</code> task.
Below are some examples of how to do this.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antProperties"></a><p class="title"><b>Example 21.13. Setting an Ant property</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties[<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'buildDir'</span>] = buildDir
ant.property(name: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'buildDir'</span>, location: buildDir)
</pre><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><echo></span>buildDir = ${buildDir}<span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"></echo></span>
</pre></div></div><br class="example-break"><p>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 <code class="literal">AntBuilder</code> instance. The Ant properties are
also available as a Map. Below are some examples.</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antProperties"></a><p class="title"><b>Example 21.14. Getting an Ant property</b></p><div class="example-contents"><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><property</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">name</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"antProp"</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">value</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"a property defined in an Ant build"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">/></span>
</pre><p><code class="filename">build.gradle</code></p><pre class="programlisting">println ant.antProp
println ant.properties.antProp
println ant.properties[<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'antProp'</span>]
</pre></div></div><br class="example-break"><p>There are several ways to set an Ant reference:</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antProperties"></a><p class="title"><b>Example 21.15. Setting an Ant reference</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.path(id: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'classpath'</span>, location: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'libs'</span>)
ant.references.classpath = ant.path(location: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'libs'</span>)
ant.references[<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'classpath'</span>] = ant.path(location: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'libs'</span>)
</pre><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><path</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">refid</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"classpath"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">/></span>
</pre></div></div><br class="example-break"><p>There are several ways to get an Ant reference:</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antProperties"></a><p class="title"><b>Example 21.16. Getting an Ant reference</b></p><div class="example-contents"><p><code class="filename">build.xml</code></p><pre class="programlisting"><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag"><path</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">id</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"antPath"</span> <span xmlns:xslthl="http://xslthl.sf.net" class="hl-attribute">location</span>=<span xmlns:xslthl="http://xslthl.sf.net" class="hl-value">"libs"</span><span xmlns:xslthl="http://xslthl.sf.net" class="hl-tag">/></span>
</pre><p><code class="filename">build.gradle</code></p><pre class="programlisting">println ant.references.antPath
println ant.references[<span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">'antPath'</span>]
</pre></div></div><br class="example-break"></div><div class="section"><div class="titlepage"><div><div><h2 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:ant_logging" class="section-anchor" href="#sec:ant_logging"></a>21.4. Ant logging</h2></div></div></div><p>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:
</p><div xmlns:xslthl="http://xslthl.sf.net" class="table"><p class="title"><b>Table 21.1. Ant message priority mapping</b></p><div class="table-contents"><table id="N121E5"><thead><tr>
<td>Ant Message Priority</td>
<td>Gradle Log Level</td>
</tr></thead><tr>
<td><em class="firstterm">VERBOSE</em></td>
<td><code class="literal">DEBUG</code></td>
</tr><tr>
<td><em class="firstterm">DEBUG</em></td>
<td><code class="literal">DEBUG</code></td>
</tr><tr>
<td><em class="firstterm">INFO</em></td>
<td><code class="literal">INFO</code></td>
</tr><tr>
<td><em class="firstterm">WARN</em></td>
<td><code class="literal">WARN</code></td>
</tr><tr>
<td><em class="firstterm">ERROR</em></td>
<td><code class="literal">ERROR</code></td>
</tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:fine_tuning_ant_logging" class="section-anchor" href="#sec:fine_tuning_ant_logging"></a>21.4.1. Fine tuning Ant logging</h3></div></div></div><p>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 <code class="literal">LIFECYCLE</code> log level, which is the
default for Gradle. Many Ant tasks log messages at the <em class="firstterm">INFO</em> priority, which means to expose
those messages from Gradle, a build would have to be run with the log level set to <code class="literal">INFO</code>,
potentially logging much more output than is desired.
</p><p>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 <code class="literal">QUIET</code>. However, this could result in other,
desirable output being suppressed.
</p><p>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 <code class="literal">LIFECYCLE</code>
log level using the <a class="ulink" href="../javadoc/org/gradle/api/AntBuilder.html#setLifecycleLogLevel(java.lang.String)" target="_top"><code class="classname">AntBuilder.setLifecycleLogLevel(java.lang.String)</code></a> method. When this value
is set, any Ant message logged at the configured priority or above will be logged at least at <code class="literal">LIFECYCLE</code>.
Any Ant message logged below this priority will be logged at most at <code class="literal">INFO</code>.
</p><p>For example, the following changes the mapping such that Ant <em class="firstterm">INFO</em> priority messages are exposed at the <code class="literal">LIFECYCLE</code>
log level.
</p><div class="example"><a xmlns:xslthl="http://xslthl.sf.net" name="antLogging"></a><p class="title"><b>Example 21.17. Fine tuning Ant logging</b></p><div class="example-contents"><p><code class="filename">build.gradle</code></p><pre class="programlisting">ant.lifecycleLogLevel = <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"INFO"</span>
task hello {
doLast {
ant.echo(level: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"info"</span>, message: <span xmlns:xslthl="http://xslthl.sf.net" class="hl-string">"hello from info priority!"</span>)
}
}
</pre><p>Output of <strong class="userinput"><code>gradle hello</code></strong></p><pre class="screen">> gradle hello
:hello
[ant:echo] hello from info priority!
BUILD SUCCESSFUL
Total time: 1 secs
</pre></div></div><br class="example-break"><p>On the other hand, if the <code class="literal">lifecycleLogLevel</code> was set to <em class="firstterm">ERROR</em>, Ant messages logged
at the <em class="firstterm">WARN</em> priority would no longer be logged at the <code class="literal">WARN</code> log level. They would now
be logged at the <code class="literal">INFO</code> level and would be suppressed by default.
</p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title"><a xmlns:xslthl="http://xslthl.sf.net" name="sec:ant_api" class="section-anchor" href="#sec:ant_api"></a>21.5. API</h2></div></div></div><p>The Ant integration is provided by <a class="ulink" href="../javadoc/org/gradle/api/AntBuilder.html" target="_top"><code class="classname">AntBuilder</code></a>.</p></div><div class="footnotes"><br><hr width="100" align="left"><div id="ftn.N1205F" class="footnote"><p><a href="#N1205F" class="para"><sup class="para">[9] </sup></a>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
</p></div></div></div><div class="navfooter"><div><div class="navbar"><a xmlns:xslthl="http://xslthl.sf.net" href="working_with_files.html" title="Working With Files - Gradle User Guide Version 3.4.1">Previous</a><span>|</span><a xmlns:xslthl="http://xslthl.sf.net" href="userguide.html" title="Gradle User Guide Version 3.4.1">Contents</a><span>|</span><a xmlns:xslthl="http://xslthl.sf.net" href="build_lifecycle.html" title="The Build Lifecycle - Gradle User Guide Version 3.4.1">Next</a></div></div></div></body></html>
|