Gradle Essentials
By Dabir Kunal and Abhinanda
()
About this ebook
About This Book
- Write beautiful build scripts for various types of projects effortlessly
- Become more productive by harnessing the power and elegance of the Gradle DSL
- Learn how to use Gradle quickly and effectively with this step-by-step guide
Who This Book Is For
This book is for Java and other JVM-based language developers who want to use Gradle or are already using Gradle on their projects.
No prior knowledge of Gradle is required, but some familiarity with build-related terminologies and an understanding of the Java language would help.
What You Will Learn
- Master the Gradle DSL by identifying the building blocks
- Learn just enough Groovy for Gradle
- Set up tests and reports for your projects to make them CI ready
- Create library, stand-alone, and web projects
- Craft multi-module projects quickly and efficiently
- Migrate existing projects to a modern Gradle build
- Extract common build logic into plugins
- Write builds for languages like Java, Groovy, and Scala
In Detail
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.
Style and approach
This step-by-step guide focuses on being productive with every chapter. When required, topics are explained in-depth to give you a good foundation of the Gradle fundamentals. The book covers most aspects of builds required for conventional JVM-based projects, and when necessary, points you towards the right resources.
Related to Gradle Essentials
Related ebooks
Mastering Gradle Rating: 0 out of 5 stars0 ratingsGradle for Android Rating: 0 out of 5 stars0 ratingsReactive State for Angular with NgRx Rating: 0 out of 5 stars0 ratingsMongoose for Application Development Rating: 5 out of 5 stars5/5Jasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsMockito Essentials Rating: 3 out of 5 stars3/5Spring Boot Cookbook Rating: 0 out of 5 stars0 ratingsGradle Effective Implementation Guide Rating: 3 out of 5 stars3/5Node.js High Performance Rating: 0 out of 5 stars0 ratingsBuilding a RESTful Web Service with Spring Rating: 5 out of 5 stars5/5Test-Driven JavaScript Development Rating: 0 out of 5 stars0 ratingsNode.js By Example Rating: 2 out of 5 stars2/5Learning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition) Rating: 0 out of 5 stars0 ratingsHands-on GitHub Actions: Implement CI/CD with GitHub Action Workflows for Your Applications Rating: 0 out of 5 stars0 ratingsDeploying Node.js Rating: 5 out of 5 stars5/5Test-Driven Java Development Rating: 4 out of 5 stars4/5GitLab Cookbook Rating: 0 out of 5 stars0 ratingsGetting Started with Grunt: The JavaScript Task Runner Rating: 3 out of 5 stars3/5Git Best Practices Guide Rating: 0 out of 5 stars0 ratingsIntelliJ IDEA Essentials Rating: 0 out of 5 stars0 ratingsLearning Angular for .NET Developers Rating: 0 out of 5 stars0 ratingsMastering Spring 5.0 Rating: 5 out of 5 stars5/5Go Programming Blueprints Rating: 0 out of 5 stars0 ratingsHacking with Spring Boot 2.4: Classic Edition: Hacking with Spring Boot, #2 Rating: 0 out of 5 stars0 ratingsTypeScript Essentials Rating: 4 out of 5 stars4/5Learn Java 12 Programming: A step-by-step guide to learning essential concepts in Java SE 10, 11, and 12 Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Mastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5TensorFlow in 1 Day: Make your own Neural Network Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5C Programming for Beginners: Your Guide to Easily Learn C Programming In 7 Days Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsThe Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsSecrets of the JavaScript Ninja Rating: 4 out of 5 stars4/5
Reviews for Gradle Essentials
0 ratings0 reviews
Book preview
Gradle Essentials - Dabir Kunal
Table of Contents
Gradle Essentials
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Running Your First Gradle Task
Installing Gradle
Installing manually
Installing on Mac OS X and Linux
Installing on Windows
Alternate methods of installing Gradle
Installing via OS-specific package managers
Mac OS X
Linux (Ubuntu)
Windows
Installing via SDKMAN
Verifying the installation
Setting JVM options
The Gradle command-line interface
The first Gradle build script
Task name abbreviation
Gradle Daemon
Gradle Wrapper
Generating wrapper files
Running a build via wrapper
Summary
2. Building Java Projects
Building a simple Java project
Creating a build file
Adding source files
Building the project
A brief introduction to plugins
Unit testing
Adding a unit test source
Adding the JUnit to the classpath
Running the test
Viewing test reports
Fitting tests in the workflow
Bundling an application distributable
Running the application with Gradle
Building the distribution archive
Generating IDE project files
Summary
3. Building a Web Application
Building a simple Java web project
Creating source files
Creating a build file
Building the artifact
Running the web application
Plugins to the rescue
References
Project dependencies
External libraries
The dynamic version
Transitive dependencies
Dependency configurations
Repositories
Summary
4. Demystifying Build Scripts
Groovy for Gradle build scripts
Why Groovy?
Groovy primer
Running Groovy code
Variables
Strings
Regular expressions
Closures
Data structures
List
Set
Map
Methods
Calling methods
Default values of parameters
Methods with map parameters/named parameters
Methods with varags
Methods with closure params
Classes
Constructors
Properties
Instance methods
Another look at applying plugins
Gradle – an object-oriented build tool
Build phases
Initialization
Configuration
Execution
Life cycle callbacks
Gradle Project API
Project methods
Project properties
Extra properties on a project
Tasks
Attaching actions to a task
Task flow control
dependsOn
finalizedBy
onlyIf
mustRunAfter and shouldRunAfter
Creating tasks dynamically
Setting default tasks
Task types
Using task types
Creating task types
References
Groovy
Gradle API and DSL used in this chapter
Summary
5. Multiprojects Build
The multiproject directory layout
The settings.gradle file
Organizing build logic in multiproject builds
Applying a build logic to all projects
Applying build logic to subprojects
Dependency on subprojects
Summary
6. The Real-world Project with Gradle
Migrating from an Ant-based project
Importing an Ant file
Using AntBuilder API
Rewriting Ant tasks to Gradle tasks
Migrating from a Maven project
Publishing artifacts
Continuous Integration
Generating documentation
Summary
7. Testing and Reporting with Gradle
Testing with TestNG
Integration testing
Code coverage
Code analysis reports
Summary
8. Organizing Build Logic and Plugins
Extracting build logic to buildSrc
The first plugin
Configuring plugins
Summary
9. Polyglot Projects
The polyglot application
Building Groovy projects
Building Scala projects
Joint compilation
References
Summary
Index
Gradle Essentials
Gradle Essentials
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2015
Production reference: 1161215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-236-3
www.packtpub.com
Credits
Authors
Kunal Dabir
Abhinandan
Reviewers
Eric Berry
André Burgaud
Michał Huniewicz
Fredrik Sandell
Commissioning Editor
Amarabha Banerjee
Acquisition Editors
Richard Brookes-Bland
Larissa Pinto
Content Development Editor
Rashmi Suvarna
Technical Editor
Madhunikita Sunil Chindarkar
Copy Editor
Trishya Hajare
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Hemangini Bari
Production Coordinator
Shantanu N. Zagade
Cover Work
Shantanu N. Zagade
About the Authors
Kunal Dabir has over 10 years of experience working with clients ranging from Fortune 500 companies to startups. Currently, he works as a Lead Consultant at ThoughtWorks. He is a Java user group's co-organizer and speaks at various meet-ups and conferences.
While he is always eager to learn a new language, he usually codes in languages such as Groovy, Scala, JavaScript, CoffeeScript, Ruby, and Java. He frequently contributes to open source projects and also hosts his own projects on GitHub.
He has always been passionate about automating and scripting. From there, he got a knack for build tools. Apart from Gradle, he has spent a fair amount of time writing build scripts with tools such as Ant, Maven, Grunt, and Gulp. He was introduced to Gradle in 2011 while using Gaelyk. Since then, Gradle has become his tool of choice for build automation.
He can be found on Twitter and GitHub as @kdabir.
Acknowledgments
First and foremost, a big thanks to my loving wife, Smita, and adorable son, Nairit. Both of them patiently tolerated me spending countless hours in front of my Mac and never complained. I would like to thank my parents for always doing everything that they could so that I could do what I like. I dedicate this book to Smita, Nairit, Aai, and Baba.
This book would not have been possible without Packt's trust in me. I would like to thank the editors and coordinators from Packt, including Richard, Parita, Priyanka, Rashmi, Madhunikita, and many more. I would also like to thank Abhinandan for providing a helping hand with the project at the time it was required the most. Also, heartfelt thanks to all the reviewers, André Burgaud, Eric Berry, Fredrik Sandell, and Michał Huniewicz, for painstakingly reviewing all the chapters and providing detailed feedback.
I am grateful to ThoughtWorks for being such an amazing place where I learned so many things.
Last but not the least, this acknowledgement can not be complete without thanking the folks who made Gradle so awesome, those who built and maintained Groovy, and the Groovy community. Kudos to all for the hard work.
Abhinandan is a Java guy with an extensive experience in software design, architecture, and deployment and automation frameworks. He is passionate about providing solutions for different business needs. His other passions include hiking, reading, and travelling. You can contact him at <designationtraveller@yahoo.com>.
Like how a film cannot be made with just actors and directors—it requires lots of different team members' help, who support at different stages until the movie gets released— a book can't be written with just the effort of one person or the author. It requires lots of support from different people at different stages, without which it would not be possible to put the thoughts on paper and make it available to the audience.
First and foremost, I would like to thank my family for all the support they gave me throughout this book. They never complained about the weekends and vacations that I compromised while working on this book.
I would like to express my gratitude to the Packt Publishing team (Parita, Purav, and Rashmi), who provided support from the initiation of the idea until the publication of the book. I appreciate that they believed in me and provided me the opportunity to become the co-author of this book.
I would like to thank the reviewers who helped me to improve the quality of this book.
Thanks to Mainak for the quality input and comments, which helped to complete this book. I could not have done it without you.
About the Reviewers
Eric Berry is the co-founder and vice president of engineering at Keeply Inc. He graduated in 2003 from Cal Poly Pomona with a BS in computer science, and has more than 11 years of full-stack development experience working for Evite (http://www.evite.com/), eHarmony (http://www.eharmony.com/), and Chegg (http://www.chegg.com/). He was first introduced to Gradle in late 2010 while working at eHarmony, and created Chegg's middle-tier SOA using Gradle for all Java-based projects. As a supporter of open source software, he's the plugin release manager for the jEdit text editor and also the original author of the Gradle-release and Gradle-templates plugins.
He has worked as a senior software engineer at Evite specializing in full-stack, JSP, Servlet, Spring Framework, Hibernate, web-2.0
JavaScript based frontend.
He has also worked as a senior software engineer at eHarmony specializing in full-stack, Java, Spring, Struts, Groovy, Spring Integration, Jersey.
He has worked as a lead software engineer at Chegg specializing in backend services, Java, Spring, Hibernate, Gradle, Jersey.
André Burgaud is a software engineer who is passionate about new technologies, programming languages in general, and Python in particular.
He started in law enforcement where he built up an interest in security. A career change led him to join the telecommunication department of the Gendarmerie headquarters in France; later, he implemented network management systems for Qwest broadband services in Minnesota, USA. He currently leads a software development department at Infinite Campus, focusing on the infrastructure for complex web applications.
During his spare time, he attempts to quench his thirst for technology by exploring programming languages, tools, operating systems, servers, or cloud services; also, he likes attending local meetups or online classes, listening to podcasts, and reading books.
Michał Huniewicz is a London-based professional software developer, amateur photo journalist, and one-time dervish. Currently, he is shifting his focus to big data challenges and has been involved in projects across a variety of industries, including banking, media, finance, telecoms, and government. He was also the head developer of an award-winning community portal. He holds an MSc degree in computer science from Adam Mickiewicz University. Learn more about him at http://www.m1key.me/.
He has also reviewed Gradle Effective Implementation Guide from Packt Publishing.
I would like to thank my friend, Bianca, for being such an amazing inspiration over the years—dziękuję.
Fredrik Sandell is a full-stack software developer with many years of experience developing Java-based web applications. He holds a MSc degree in networks and distributed systems from the Chalmers University of Technology and is currently based in Stockholm, Sweden.
Fredrik is employed at a fantastic company called Squeed AB.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today