Chapter 6
The Project and Task API

6.1 Project API

In the tutorial in chapter 2 we have called for example the method createTask. Where does this method come from? We said earlier that the directory containing the build script defines a project for Gradle. For Gradle this means, that it creates an instance of org.gradle.api.Project and associates it with the build script. With the build script you can configure this project object.

Let’s try this out and try to access the name property of the Project object.

  createTask('check') {
      println project.name
      println name
  }

  >gradle -q -bprojectApi.gradle check
  tutorial
  tutorial

Both println statements print out the same property. One uses the auto delegation to the project object, for properties not defined in the build script. The other statement uses the project property available to any build script, which provides an instance of the associated project object. Only if you define a property or a method which has the same name as a member of the project object, you need to use the project property. Look here to learn more about org.gradle.api.Project.

6.2 Task API

Many of the methods of the project object return task objects. We have already seen some ways on how to use the task objects in chapter 2. Look here to learn more about org.gradle.api.Task.

6.3 Summary

The project and the task API constitute the core layer of Gradle and provide all the possible interaction options with this layer.1 This core-layer constitutes a language for dependency based programming.2 There are many other projects providing such a language. There is Ant for Java, Rake and Rant for Ruby, SCons for Python, the good old Make and many more.3 We think that one thing that makes Gradle special compared to the other tools, is its strong support for applying dependency based programming on multiproject builds. We also think that just Gradles core layer (together with its integration of the Ant tasks), provides a more convenient build system than Ant’s core layer.