Gradle Beyond the Basics
Tim Berglund
Gradle Beyond the Basics
by Tim Berglund
Copyright © 2013 Gradle, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Mike Loukides and Meghan Blanchette
Production Editor: Kara Ebrahim
Proofreader: Kara Ebrahim
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Rebecca Demarest
July 2013:
First Edition
Revision History for the First Edition:
2013-07-15: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449304676 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Gradle Beyond the Basics, the image of a Belgian shepherd dog, and related trade dress are
trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-30467-6
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
1. File Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Copy Task 1
Transforming Directory Structure 3
Renaming Files During Copy 3
Filtering and Transforming Files 4
Keyword Expansion 4
Filtering Line by Line 6
Filtering File by File 8
The File Methods 9
file() 9
files() 11
fileTree() 12
The FileCollection Interface 12
Converting to a Set 14
Converting to a Path String 14
Module Dependencies as FileCollections 15
Adding and Subtracting FileCollections 16
SourceSets as FileCollections 17
Lazy Files 18
Conclusion 19
2. Custom Plug-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Plug-In Philosophy 21
The Plug-In API 22
The Example Plug-In 22
Setup 23
Sketching Out Your Plug-In 24
iii
Custom Liquibase Tasks 24
Applying Yourself 25
Extensions 26
Packaging a Plug-In 31
Conclusion 33
3. Build Hooks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Gradle Lifecycle: A Review 35
Advising the Build Graph 36
Advising Project Evaluation 36
Global Project Loading and Evaluation Hooks 38
Build Finished 39
Rules 41
Creating a Rule 42
Dealing with Imperative Rule Code 43
Generalizing Rules Beyond Tasks 45
Conclusion 46
4. Dependency Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
What Is Dependency Management? 47
Dependency Concepts 48
Configurations 49
Module Dependencies 51
Dynamic Versions 53
File Dependencies 53
Project Dependencies 54
Internal Dependencies 55
Repositories: Dependency Resolution 56
Maven Repositories 56
Ivy 59
Repository Credentials 60
Static Dependencies 61
Buildscript Dependencies 63
Dependency Caching 64
Configuring Resolution Strategy 66
Failing on Version Conflict 66
Forcing Versions 66
Cache Expiration 67
Conclusion 67
Afterword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
iv
|
Table of Contents
Preface
Welcome to Gradle Beyond the Basics, the second book in the O’Reilly Gradle series.
This book picks up from its predecessor, Building and Testing with Gradle, and takes
you further into the Gradle programming model. We offer the recipes, techniques, and
syntax that make Gradle more than just free-form scripting, and place your build instead
on the foundation of a strong domain model. The difference is, it is your domain model,
not a generic one from some build tool that is ignorant of the specifics of your project.
Having introduced you to the basic elements of Gradle in the first book, we can begin
to explore the tool’s capabilities a bit more deeply. We will cover four discrete areas of
Gradle functionality: file operations, custom Gradle plug-ins, build lifecycle hooks, and
dependency management. We assume that you are familiar with the basics of how to
use Gradle, and with a keen respect of your time and interest, offer no further intro‐
duction to the mechanics of simple Gradle builds. If you are brand new to the topic,
you should definitely read Building and Testing first.
The Gradle APIs are rich, the possibilities for DSLs matching your domain are abundant,
and the path towards finally having a build system that conforms to your product is
clear. Let’s move forward.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
v
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Safari® Books Online
Safari Books Online is an on-demand digital library that delivers
expert content in both book and video form from the world’s lead‐
ing authors in technology and business.
Technology professionals, software developers, web designers, and business and crea‐
tive professionals use Safari Books Online as their primary resource for research, prob‐
lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐
zations, government agencies, and individuals. Subscribers have access to thousands of
books, training videos, and prepublication manuscripts in one fully searchable database
from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐
fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John
Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT
Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐
ogy, and dozens more. For more information about Safari Books Online, please visit us
online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
vi
| Preface