Book Image

Gradle Essentials

By : Abhinandan Maheshwari
Book Image

Gradle Essentials

By: Abhinandan Maheshwari

Overview of this book

Gradle is an advanced and modern build automation tool. It inherits the best elements of the past generation of build tools, but it also differs and innovates to bring terseness, elegance, simplicity, and the flexibility to build. Right from installing Gradle and writing your first build file to creating a fully-fledged multi-module project build, this book will guide you through its topics in a step-by-step fashion. You will get your hands dirty with a simple Java project built with Gradle and go on to build web applications that are run with Jetty or Tomcat. We take a unique approach towards explaining the DSL using the Gradle API, which makes the DSL more accessible and intuitive. All in all, this book is a concise guide to help you decipher the Gradle build files, covering the essential topics that are most useful in real-world projects. With every chapter, you will learn a new topic and be able to readily implement your build files.
Table of Contents (17 chapters)
Gradle Essentials
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

The Gradle command-line interface


Gradle, just like other build tools, is primarily run from a command line. That's why it is worth spending some time to get familiar with its command-line interface. Typically, a gradle command is issued from the root of a project directory with some tasks to be executed. Let's say we are in the hello-gradle directory, which is currently empty.

Gradle provides a very simple command-line interface (CLI), which takes the following form:

gradle [options…] [tasks…]

As we can see, apart from the gradle command itself, everything else is optional. The options tweak the execution of the Gradle whereas tasks, which we will see in detail later, are the basic units of work. Options are common across all projects and are specific to Gradle but tasks may vary depending on the project in which the gradle command is being run.

There are some tasks that are available on all projects. One such task is help:

$ gradle help
:help

Welcome to Gradle 2.9.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL

Total time: 0.639 secs

Gradle is helping us out by telling us how to find all the available tasks and list all command-line options. Let's first check what other tasks are currently available on our project. Remember we are still in the empty directory hello-gradle:

$ gradle tasks
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
components - Displays the components produced by root project 'hello-gradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'hello-gradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'hello-gradle'.
help - Displays a help message.
model - Displays the configuration model of root project 'hello-gradle'. [incubating]
projects - Displays the sub-projects of root project 'hello-gradle'.
properties - Displays the properties of root project 'hello-gradle'.
tasks - Displays the tasks runnable from root project 'hello-gradle'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL

Total time: 0.652 secs

This shows us some generic tasks that are available even without us adding any task to our project. We can try running all these tasks and see the output. We will see these tasks in details in the upcoming chapters.

The other useful command gradle help suggested us to check all the available options with the --help option.

Tip

The help task is not the same as the --help option.

When we run the gradle --help command, we get the following output:

$ gradle --help

USAGE: gradle [option...] [task...]

-?, -h, --help          Shows this help message.
-a, --no-rebuild        Do not rebuild project dependencies.
-b, --build-file        Specifies the build file.
…..

(The output is truncated for brevity.)

The option has a long form such as --help and may have a short from such as -h. We have already used one option before, that is --version or -v, which prints information about the Gradle version. The following are some commonly used options; there are many more options, which can be seen using the gradle --help command:

Options

Description

-b, --build-file

This specifies a build file (default: build.gradle)

--continue

This continues task execution even after a task failure

-D, --system-prop

This sets the system property of the JVM

-d, --debug

This prints debug level logs

--gui

This starts Gradle GUI

-i, --info

This prints info level logs

-P, --project-prop

This adds a property to the project

-q, --quiet

This logs only errors

-s, --stacktrace

This prints stack traces for exceptions

-x, --exclude-task

This excludes a specific task