| 1 |
////////////////////////////////////////// |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
or more contributor license agreements. See the NOTICE file |
| 5 |
distributed with this work for additional information |
| 6 |
regarding copyright ownership. The ASF licenses this file |
| 7 |
to you under the Apache License, Version 2.0 (the |
| 8 |
"License"); you may not use this file except in compliance |
| 9 |
with the License. You may obtain a copy of the License at |
| 10 |
|
| 11 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
|
| 13 |
Unless required by applicable law or agreed to in writing, |
| 14 |
software distributed under the License is distributed on an |
| 15 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
KIND, either express or implied. See the License for the |
| 17 |
specific language governing permissions and limitations |
| 18 |
under the License. |
| 19 |
|
| 20 |
////////////////////////////////////////// |
| 21 |
|
| 22 |
= AntBuilder |
| 23 |
|
| 24 |
Despite being primarily a build tool, http://ant.apache.org/[Apache Ant] is |
| 25 |
a very practical tool for manipulating files including zip files, copy, resource processing, ... |
| 26 |
But if ever you’ve been working with a `build.xml` file or some _Jelly script_ |
| 27 |
and found yourself a little restricted by all those pointy brackets, or |
| 28 |
found it a bit weird using XML as a scripting language and wanted |
| 29 |
something a little cleaner and more straight forward, then maybe Ant |
| 30 |
scripting with Groovy might be what you're after. |
| 31 |
|
| 32 |
Groovy has a helper class called `AntBuilder` which makes the scripting |
| 33 |
of Ant tasks really easy; allowing a real scripting language to be used |
| 34 |
for programming constructs (variables, methods, loops, logical |
| 35 |
branching, classes etc). It still looks like a neat concise version of |
| 36 |
Ant's XML without all those pointy brackets; though you can mix and |
| 37 |
match this markup inside your script. Ant itself is a collection of jar |
| 38 |
files. By adding them to your classpath, you can easily use them within |
| 39 |
Groovy as is. We believe using `AntBuilder` leads to more concise and |
| 40 |
readily understood syntax. |
| 41 |
|
| 42 |
`AntBuilder` exposes Ant tasks directly using the convenient builder notation that |
| 43 |
we are used to in Groovy. Here is the most basic example, which is printing a message |
| 44 |
on the standard output: |
| 45 |
|
| 46 |
[source,groovy] |
| 47 |
---- |
| 48 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=example_echo,indent=0] |
| 49 |
---- |
| 50 |
<1> creates an instance of `AntBuilder` |
| 51 |
<2> executes the `echo` task with the message in parameter |
| 52 |
|
| 53 |
Imagine that you need to create a ZIP file: |
| 54 |
|
| 55 |
[source,groovy] |
| 56 |
---- |
| 57 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=create_zip_builder,indent=0] |
| 58 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=create_zip_zip,indent=0] |
| 59 |
---- |
| 60 |
|
| 61 |
In the next example, we demonstrate the use of `AntBuilder` to copy a list of files |
| 62 |
using a classical Ant pattern directly in Groovy: |
| 63 |
|
| 64 |
[source,groovy] |
| 65 |
---- |
| 66 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=copy_files,indent=0] |
| 67 |
---- |
| 68 |
|
| 69 |
Another example would be iterating over a list of files matching a specific pattern: |
| 70 |
|
| 71 |
[source,groovy] |
| 72 |
---- |
| 73 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=filescanner,indent=0] |
| 74 |
---- |
| 75 |
|
| 76 |
Or execute a JUnit test: |
| 77 |
|
| 78 |
[source,groovy] |
| 79 |
---- |
| 80 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=run_junit,indent=0] |
| 81 |
---- |
| 82 |
|
| 83 |
We can even go further by compiling and executing a Java file directly from Groovy: |
| 84 |
|
| 85 |
[source,groovy] |
| 86 |
---- |
| 87 |
include::{rootProjectDir}/subprojects/groovy-ant/src/spec/test/builder/AntBuilderSpecTest.groovy[tags=compile_java,indent=0] |
| 88 |
---- |
| 89 |
|
| 90 |
It is worth mentioning that `AntBuilder` is included in http://gradle.org/[Gradle], so you can use it in Gradle |
| 91 |
just like you would in Groovy. Additional documentation can be found in the |
| 92 |
http://gradle.org/docs/current/userguide/ant.html[Gradle manual]. |