Reader small image

You're reading from  Gradle Effective Implementations Guide - Second Edition

Product typeBook
Published inMay 2016
Reading LevelIntermediate
Publisher
ISBN-139781784394974
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Hubert Klein Ikkink
Hubert Klein Ikkink
author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink

Right arrow

Chapter 3.  Working with Gradle Build Scripts

A Gradle script is a program. We use a Groovy DSL to express our build logic. Gradle has several useful built-in methods to handle files and directories as we often deal with files and directories in our build logic.

In this chapter, we will discuss how to use Gradle's features to work with files and directories. We will also take a look at how to set properties in a Gradle build and use Gradle's logging framework. Finally, we see will how to use the Gradle Wrapper task to distribute a configurable Gradle with our build scripts.

Working with files


It is very common in a build script that we have to work with files and directories. For example, when we need to copy a file from one directory to another or create a directory to store the output of a task or program.

Locating files

To locate a file or directory relative to the current project, we can use the file() method. This method is actually a method of the Project object that is connected to our build script. In the previous chapter, we discussed how to use an explicit reference to the project variable or simply invoke methods and properties of the Project object implicitly.

The file() method will resolve the location of a file or directory relative to the current project and not the current working directory. This is very useful as we can run a build script from a different directory than the location of the actual build script. File or directory references that are returned by the file() method are then resolved, relative to the project directory.

We can pass any...

Project properties


In a Gradle build file, we can access several properties that are defined by Gradle, but we can also create our own properties. We can set the value of our custom properties directly in the build script and we can also do this by passing values via the command line.

The default properties that we can access in a Gradle build are displayed in the following table:

Using logging


In Chapter 1Starting with Gradle, we discussed several command-line options that we can use to show either more or fewer log messages when we run a Gradle build. These messages were from the Gradle internal tasks and classes. We used a println method in our Gradle build scripts to see some output, but we can also use Gradle's logging mechanisms to have a more customizable way to define logging messages.

Gradle supports several logging levels that we can use for our own messages. The level of our messages is important as we can use the command-line options to filter the messages for log levels.

The following table shows the log levels that are supported by Gradle:

Name

Type

Default value

project

Project

The project instance.

name

String

The name of the project directory. The name is read-only.

path

String

The absolute path of the project.

description

String

The description of the project.

projectDir

File

The directory containing the build script. The value is read-only.

buildDir

File

The directory with the build name in the directory, containing the build script.

rootDir

File

The directory of the project at the root of a project structure.

group

Object

Not specified.

version

Object

Not specified.

ant

AntBuilder...

Level

Used for

DEBUG

Debug messages

INFO

Information messages

LIFECYCLE

Progress information messages

WARNING

Warning messages

QUIET

Import information messages

ERROR

Error messages

Every Gradle build file and task has a logger object. The logger object is an instance of a Gradle-specific...

Using the Gradle Wrapper


Normally, if we want to run a Gradle build, we must have Gradle installed on our computer. Also, if we distribute our project to others and they want to build the project, they must have Gradle installed on their computers. The Gradle Wrapper can be used to allow others to build our project even if they don't have Gradle installed on their computers.

The wrapper is a batch script on the Microsoft Windows operating systems or shell script on other operating systems that will download Gradle and run the build using the downloaded Gradle.

By using the wrapper, we can make sure that the correct Gradle version for the project is used. We can define the Gradle version, and if we run the build via the wrapper script file, the version of Gradle that we defined is used.

Creating wrapper scripts

To create the Gradle Wrapper batch and shell scripts, we can invoke the built-in wrapper task. This task is already available if we have installed Gradle on our computer. Let's invoke...

Summary


In this chapter, we discussed the support that Gradle gives when working with files. We saw how to create a file or directory and a collection of files and directories. A file tree represents a hierarchical set of files.

We can add logging messages to our project and tasks and see the output when we run a Gradle build. We discussed how to use different log levels to influence how much information is shown in the output. We also used LoggingManager to capture the standard output and error messages and redirect them to custom log levels.

We discussed how to use the Gradle Wrapper to allow users to build our projects even if they don't have Gradle installed. We discussed how to customize the Wrapper to download a specific version of Gradle and use it to run our build.

In the next chapter, we will create a Java project and use the Java plugin to add a set of default tasks that we can use to compile, test, and package our Java code.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Gradle Effective Implementations Guide - Second Edition
Published in: May 2016Publisher: ISBN-13: 9781784394974
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Hubert Klein Ikkink

Hubert Klein Ikkink was born in 1973 and lives in Tilburg, the Netherlands, with his beautiful wife and gorgeous children. He is also known as mrhaki, which is simply the initials of his name prepended by mr. He studied Information Systems and Management at the Tilburg University. After finishing his studies he started to work at a company which specialized in knowledge-based software. There he started writing his first Java software (yes, an applet!) in 1996. Over the years his focus switched from applets, to servlets, to Java Enterprise Edition applications, to Spring-based software. In 2008 he wanted to have fun again when writing software. The larger projects he was working on were more about writing configuration XML files, tuning performance and less about real development in his eyes. So he started to look around and noticed Groovy as a good language to learn about. He could still use existing Java code, libraries, and his Groovy classes in Java. The learning curve isn’t steep and to support his learning phase he wrote down interesting Groovy facts in his blog with the title Groovy Goodness. He posts small articles with a lot of code samples to understand how to use Groovy. Since November 2011 he is also a DZone Most Valuable Blogger (MVB); DZone also posts his blog items on their site. In 2010, 2011, and 2012 Hubert was invited to speak at Gr8Conf in Copenhagen, Denmark. This is a very good conference with all the project leaders of Groovy and Groovy-related projects. In November 2010 he presented a Gradle talk at the J-Fall conference of the Dutch Java User Group. In November 2011 he presented about the new features in Groovy 1.8 at the same conference. The conference is visited by 1000 Java developers and he got the chance to educate some of them about the greatness of Gradle and Groovy. Hubert works for a company called JDriven in the Netherlands. JDriven focuses on technologies that simplify and improve development of enterprise applications. Employees of JDriven have years of experience with Java and related technologies and are all eager to learn about new technologies. Hubert works on projects using Grails and Java combined with Groovy and Gradle.
Read more about Hubert Klein Ikkink