Book Image

Gradle Effective Implementations Guide - Second Edition

By : Hubert Klein Ikkink
Book Image

Gradle Effective Implementations Guide - Second Edition

By: Hubert Klein Ikkink

Overview of this book

Gradle is a project automation tool that has a wide range of applications. The basic aim of Gradle is to automate a wide variety of tasks performed by software developers, including compiling computer source code to binary code, packaging binary codes, running tests, deploying applications to production systems, and creating documentation. The book will start with the fundamentals of Gradle and introduce you to the tools that will be used in further chapters. You will learn to create and work with Gradle scripts and then see how to use Gradle to build your Java Projects. While building Java application, you will find out about other important topics such as dependency management, publishing artifacts, and integrating the application with other JVM languages such as Scala and Groovy. By the end of this book, you will be able to use Gradle in your daily development. Writing tasks, applying plugins, and creating build logic will be your second nature.
Table of Contents (18 chapters)
Gradle Effective Implementations Guide - Second Edition
About the Author
About the Reviewer

Default Gradle tasks

We can create our simple build script with one task. We can ask Gradle to show us the available tasks for our project. Gradle has several built-in tasks that we can execute. We type gradle -q tasks to see the tasks for our project:

$ gradle -q 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-world'. [incubating]
dependencies - Displays all dependencies declared in root project  'hello-world'.
dependencyInsight - Displays the insight into a specific n      dependency in root project 'hello-world'.
help - Displays a help message.
model - Displays the configuration model of root project   'hello-world'. [incubating]
projects - Displays the sub-projects of root project 'hello-world'.
properties - Displays the properties of root project 'hello-world'.
tasks - Displays the tasks runnable from root project 'hello-world'.
Other tasks
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>

Here, we see our helloWorld task in the Other tasks section. The Gradle built-in tasks are displayed in the Help tasks section. For example, to get some general help information, we execute the help task:

$ gradle -q help
Welcome to Gradle 2.12.
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>

The properties task is very useful to see the properties available for our project. We haven't defined any property ourselves in the build script, but Gradle provides a lot of built-in properties. The following output shows some of the properties:

$ gradle -q properties
Root project
allprojects: [root project 'hello-world']
ant: org.gradle.api.internal.project.DefaultAntBuilder@3bd3d05e
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@6aba5d30
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@61d34b4
asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@588307f7
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@7df76d99

buildDir: /Users/mrhaki/Projects/gradle-effective-implementation-guide-2/gradle-impl-guide-2/src/docs/asciidoc/Chapter1/Code_Files/hello-world/build
buildFile: /Users/mrhaki/Projects/gradle-effective-implementation-guide-2/gradle-impl-guide-2/src/docs/asciidoc/Chapter1/Code_Files/hello-world/build.gradle
buildScriptSource: org.gradle.groovy.scripts.UriScriptSource@459cfcca
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@2acbc859
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@6ab7ce48
components: []
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@2c6aed22

The dependencies task will show dependencies (if any) for our project. Our first project doesn't have any dependencies, as the output shows when we run the task:

$ gradle -q dependencies
Root project
No configurations

The projects tasks will display subprojects (if any) for a root project. Our project doesn't have any subprojects. Therefore, when we run the projects task, the output shows us our project has no subprojects:

$ gradle -q projects
Root project
Root project 'hello-world'
No sub-projects
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

The model tasks displays information about the model that Gradle builds internally from our project build file. This feature is incubating, which means that the functionality can change in future versions of Gradle:

$ gradle -q model
Root project
+ model
+ tasks
     | Type:       org.gradle.model.ModelMap<org.gradle.api.Task>
        | Creator:     Project.<init>.tasks()
        + components
              | Type:       org.gradle.api.reporting.components.ComponentReport
              | Value:      task ':components'
              | Creator:     tasks.addPlaceholderAction(components)
              | Rules:
                ⤷ copyToTaskContainer
        + dependencies
              | Type:       org.gradle.api.tasks.diagnostics.DependencyReportTask
              | Value:      task ':dependencies'
              | Creator:     tasks.addPlaceholderAction(dependencies)
              | Rules:

We will discuss more about these and the other tasks in this book.