In Gradle, projects and tasks are two important concepts. A Gradle build always consists of one or more projects A project defines some sort of component we want to build. There are no defining rules about what the component is. It can be a JAR file with utility classes to be used in other projects, or a web application to be deployed to the corporate intranet. A project doesn't have to be about building and packaging code; it can also be about doing things such as copying files to a remote server or deployment of applications to servers.
A project has one or more tasks. A task is a small piece of work that is executed when we run a build, for example, compiling source code, packaging code in an archive file, generating documentation, and so on.
In this chapter we will learn how to define a project with tasks and use it as a Gradle build.
In the first chapter we have already written our first build script. Let's create a similar build script with a simple task. Gradle will look for a file with the name build.gradle
, in the current directory.
The file build.gradle
contains the tasks that make up our project. In this example, we define a simple task that prints out a simple message to the console:
project.description = 'Simple project' task simple << { println 'Running simple task for project ' + project.description }
If we run the build we see the following output in the console:
$ gradle simple :simple Running simple task for project Simple project BUILD SUCCESSFUL Total time: 2.08 secs
A couple of interesting things happen with this small build script. Gradle reads the script file and creates a Project
object. The build script configures the Project
object, and finally the set of tasks to be executed is determined and executed.
So, it is important to note that Gradle creates a Project
object for us. The Project
object has several properties and methods, and it is available in our build scripts. We can use the variable name project
to reference the Project
object, but we can also leave out this variable name to reference properties and methods of the Project
object. Gradle will automatically try to map properties and methods in the build script to the Project
object.
In our simple build script we assign the value Simple project
to the project property description
. We used the explicit project variable name and the Groovy property assignment syntax. The following build script uses a different syntax to get the same result:
setDescription("Simple project") task simple << { println 'Running simple task for project ' + project.getDescription() }
Here, we use the Java syntax to set and get the value of the description property of the project object. We are very flexible in our syntax, but we will stick with the Groovy syntax for the rest of the book, because it results in more readable build scripts.
18.191.189.186