Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

6

Build Script Basics

6.1. Projects and tasks

Everything in Gradle sits on top of two basic concepts: projects and tasks.

Every Gradle build is made up of one or more projects. A project represents some component of your software which can be built. What this means exactly depends on what it is that you are building. For example, a project might represent a library JAR or a web application. It might represent a distribution ZIP assembled from the JARs produced by other projects. A project does not necessarily represent a thing to be built. It might represent a thing to be done, such as deploying your application to staging or production environments. Don't worry if this seems a littl vague for now. Gradle's build-by-convention support adds a more concrete definition for what project is.

Each project is made up of one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating javadoc, or publishing some archives to a repository.

For now, we will look at defining some simple tasks in a build with one project. Later chapters will look at working with multiple projects and more about working with projects and tasks.

6.2. Hello world

You run a Gradle build using the gradle command. The gradle command looks for a file called bui in the current directory. [2] We call this build.gradle file a build script, although strictly speaking it is a build configuration script, as we will see later. The build script defines a project and its tasks.

To try this out, create the following build script named build.gradle.

Page 26 of 343

What does -q do?
Most of the examples in this user guide are run with the -q command-line option.
This suppresses Gradle's log messages, so that only the output of the tasks is shown. This keeps the example output in this user guide a little clearer. You don't need to use this option if you don't want. See Chapter 19, Logging for more details about the command-line options which affect Gradle's output.

Example 6.1. The first build script

build.gradle

task hello { doLast {

println 'Hello world!'

}

}

In a command-line shell, enter into the containing directory and execute the build script by running

:

Example 6.2. Execution of a build script

Output of gradle -q hello

> gradle -q hello Hello world!

What's going on here? This build script defines a single task, called hello, and adds an action to it. When you run

gradle hello, Gradle executes the hello task, which in turn executes the action you've provided. The action is simply a closure containing some Groovy code to execute.

If you think this looks 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 than 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, such as javac or copy, tasks. So when we

talk about tasks, we always mean Gradle tasks, which are the equivalent to Ant's targets. If we tal about Ant tasks (Ant commands), we explicitly say ant task.

6.3. A shortcut task definition

There is a shorthand way to define a task like our hello task above, which is more concise.

Example 6.3. A task definition shortcut

build.gradle

task hello << {

println 'Hello world!'

}

Again, this defines a task called hello with a single closure to execute. We will use this task

Page 27 of 343

definition style throughout the user guide.

6.4. Build scripts are code

Gradle's build scripts expose to you the full power of Groovy. As an appetizer, have a look at this:

Example 6.4. Using Groovy in Gradle's tasks

build.gradle

task upper << {

String someString = 'mY_nAmE' println "Original: " + someString

println "Upper case: " + someString.toUpperCase()

}

Output of gradle -q upper

> gradle -q upper Original: mY_nAmE Upper case: MY_NAME

or

Example 6.5. Using Groovy in Gradle's tasks

build.gradle

task count << {

4.times { print "$it " }

}

Output of gradle -q count

> gradle -q count 0 1 2 3

6.5. Task dependencies

As you probably have guessed, you can declare dependencies between your tasks.

Page 28 of 343

Example 6.6. Declaration of dependencies between tasks

build.gradle

task hello << {

println 'Hello world!'

}

task intro(dependsOn: hello) << { println "I'm Gradle"

}

Output of gradle -q intro

> gradle -q intro Hello world!

I'm Gradle

To add a dependency, the corresponding task does not need to exist.

Example 6.7. Lazy dependsOn - the other task does not exist (yet)

build.gradle

task taskX(dependsOn: 'taskY') << { println 'taskX'

}

task taskY << { println 'taskY'

}

Output of gradle -q taskX

> gradle -q taskX taskY

taskX

The dependency of taskX to taskY is declared before taskY is defined. This is very important for multi-project builds. Task dependencies are discussed in more detail in Section 17.4, “Addin dependencies to a task”.

Please notice, that you can't use a shortcut notation (seeSection 6.8, “Shortcut notations”)when referring to task, which is not defined yet.

6.6. Dynamic tasks

The power of Groovy can be used for more than defining what a task does. For example, you can also use it to dynamically create tasks.

Page 29 of 343

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]