In Gradle everything revolves around tasks. The tasks for your build are defined in the build script. To try this out, create the following build script named build.gradle and enter with your shell into the containing directory.
Now execute the build script1 2 :
If you think this looks damn similar to Ant’s targets, well, you are right. Gradle tasks are the equivalent to Ant targets. But as you will see, they are much more powerful. We have used a different terminology to Ant as we think the word ’task’ is more expressive than the word ’target’. Unfortunately this introduces a terminology clash with Ant, as Ant calls its commands, like javac or copy, task. So if we talk about tasks, we always mean Gradle tasks, which are the equivalent to Ant’s targets. If we talk about Ant tasks (Ant commands), we explicitly say ant task.
Gradles build scripts expose to you the full power of Groovy. As an appetizer, have a look at this:
or
As you probably have guessed, you can declare dependencies between your tasks.
To add a dependency, the corresponding task does not need to exist.
The dependency of targetX to targetY is declared before targetY is created. This is very important for multi-project builds.
The power of Groovy can not only be used inside the tasks. You can use it for example to dynamically create tasks.
Once tasks are created they can be accessed via an API. This is different to Ant. For example you can create additional dependencies.
Or you can add behavior to an existing task.
The calls doFirst and doLast can be executed multiple times. What they do is to add an action to the beginning or the end of the tasks actions list.
There is a convenient notation for accessing existing tasks.
This enables very readable code. Especially when using the out of the box tasks provided by the plugins (e.g. compile).
Let’s talk a little bit about Gradles Ant integration. Ant can be divided into two layers. The first layer is the Ant language. It contains the syntax for the build.xml, the handling of the targets, special constructs like macrodefs, etc. Basically everything except the Ant tasks and types. Gradle does not offer any special integration for this first layer. Of course you can in your build script execute an Ant build as an external process. Your build script may contain statements like: "ant clean compile".execute()3 .
The second layer of Ant is its wealth of Ant tasks and types, like javac, copy or jar. For this layer Gradle provides excellent integration simply by relying on Groovy. Groovy is shipped with the fantastic AntBuilder. Using Ant tasks from Gradle is as convenient and more powerful than using Ant tasks from a build.xml file. Let’s look at an example:
In your build script, a property called ant is provided by Gradle. It is a reference to an instance of Groovys AntBuilder. The AntBuilder is used the following way:
To learn more about the Ant Builder have a look in GINA 8.4 or at the Groovy Wiki
Gradle scales in how you can organize your build logic. The first level of organizing your build logic for the example above, is extracting a method.
Later you will see that such methods can be shared among subprojects in multi-project builds. If your build logic becomes more complex, Gradle offers you other very convenient ways to organize it. We have devoted a whole chapter to this. See Chapter 15.
As we describe in full detail later (See chapter 13) Gradle has a configuration phase and an execution phase. After the configuration phase Gradle knows all tasks that should be executed. Gradle offers you a hook to make use of this information. A usecase for this would be to check if the release task is part of the tasks to be executed. Depending on this you can assign different values to some variables.
The important thing is, that the fact that the release task has been choosen, has an effect before the release task gets executed. Nor has the release task to be the primary task (i.e. the task passed to the gradle command).
This is not the end of the story for tasks. So far we have worked with simple tasks. Tasks will be revisited in chapter 7 and when we look at the Java Plugin in chapter 9.