Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Gradle Effective Implementation Guide

You're reading from   Gradle Effective Implementation Guide A must-read for Java developers, this book will bring you bang up to date in the techniques of build automation using Gradle. A fully hands-on approach makes learning natural and entertaining.

Arrow left icon
Product type Paperback
Published in Oct 2012
Publisher Packt
ISBN-13 9781849518109
Length 382 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (20) Chapters Close

Gradle Effective Implementation Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Starting with Gradle 2. Creating Gradle Build Scripts FREE CHAPTER 3. Working with Gradle Build Scripts 4. Using Gradle for Java Projects 5. Dependency Management 6. Testing, Building, and Publishing Artifacts 7. Multi-project Builds 8. Mixed Languages 9. Maintaining Code Quality 10. Writing Custom Tasks and Plugins 11. Using Gradle with Continuous Integration 12. IDE Support Index

Writing our first build script


We now have a running Gradle installation. It is time to create our first Gradle build script. Gradle uses the concept of projects to define a related set of tasks. A Gradle build can have one or more projects. A project is a very broad concept in Gradle, but it is mostly a set of components we want to build for our application.

A project has one or more tasks. Tasks are a unit of work that need to be executed by the build. Examples of tasks are compiling source code, packaging class files into a JAR file, running tests, or deploying the application.

We now know that a task is part of a project, so to create our first task we also create our first Gradle project. We use the gradle command to run a build. Gradle will look for a file named build.gradle in the current directory. This file is the build script for our project. We define those of our tasks that need to be executed in this build script file.

We create a new file, build.gradle, and open it in a text editor. We type the following code to define our first Gradle task:

task helloWorld << {
  println 'Hello world.'
}

With this code we define a helloWorld task. The task will print the words "Hello world." to the console. println is a Groovy method to print text to the console and is basically a shorthand version of the Java method System.out.println.

The code between the brackets is a closure. A closure is a code block that can be assigned to a variable or passed to a method. Java doesn't support closures, but Groovy does. And because Gradle uses Groovy to define the build scripts, we can use closures in our build scripts.

The << syntax is, technically speaking, operator shorthand for the method leftShift(), which actually means "add to". So, we are defining here that we want to add the closure (with the statement println 'Hello world.') to our task with the name helloWorld.

First we save build.gradle, and then with the command gradle helloWorld, we execute our build:

hello-world $ gradle helloWorld
:helloWorld
Hello world.

BUILD SUCCESSFUL

Total time: 2.047 secs

The first line of output shows our line Hello world. Gradle adds some more output, such as the fact that the build was successful and the total time of the build. Because Gradle runs in the JVM, it must be started each time we run a Gradle build.

We can run the same build again, but with only the output of our task, by using the Gradle command-line option --quiet (or -q). Gradle will suppress all messages except error messages. When we use --quiet (or -q), we get the following output:

hello-world $ gradle --quiet helloWorld
Hello world.
lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image