Book Image

Gradle Essentials

By : Abhinandan Maheshwari
Book Image

Gradle Essentials

By: Abhinandan Maheshwari

Overview of this book

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.
Table of Contents (17 chapters)
Gradle Essentials
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

When I first came across Gradle in 2011, it was a young yet powerful tool. If I remember correctly, the version was 0.9. It was difficult for me to get started despite Gradle having an adequate official documentation. What I missed the most was a guide that would just help me understand the core concepts first, without having to go through the entire documentation.

Gradle is a fantastic build tool. There is so much to learn about it that new users are often clueless about where to start. It is unwise to expect an application developer to go through the entire Gradle reference material just to understand the basics.

This book attempts to help a reader get started with Gradle by revealing the key concepts in a step-by-step manner. It introduces a more advanced topic succinctly. This book focuses on the practical usage of Gradle that a reader can immediately put to use on his or her project. This book strives to stay true to the spirit of 'essentials' by avoiding going into every possible feature and option that Gradle has to offer. Code samples for applications have been consciously kept very small in order to avoid distractions from application logic.

This book is a quick start guide for Gradle. If you are a Java developer already building your code with Ant or Maven and want to switch to Gradle, this book helps you to quickly understand the different concepts of Gradle. Even if you do not have exposure to other build tools such as Ant or Maven, you can start afresh on Gradle with the help of this book. It starts with the basics of Gradle and then gently moves to concepts such as multimodule projects, migration strategies, testing strategies, Continuous Integration, and code coverage with the help of Gradle.

What this book covers

This book can be roughly divided into three parts.

Section 1 includes Chapter 1, Running Your First Gradle Task, Chapter 2, Building Java Projects, and Chapter 3, Building a Web Application. This section introduces the basics of Gradle, with very simple examples, which helps readers to create build files for Java projects and Web applications. It gives a gentle start without involving any complex concepts.

Section 2 includes Chapter 4, Demystifying Build Scripts, and Chapter 5, Multiprojects Build. This section helps the reader to understand the underpinning of Gradle in more depth, still maintaining the 'essentials' aspect of this book. It also helps the reader to understand how to interpret and write scripts that conform to Gradle DSL.

Section 3 includes Chapter 6, The Real-world Project with Gradle, Chapter 7, Testing and Reporting with Gradle, Chapter 8, Organizing Build Logic and Plugins, and Chapter 9, Polyglot Projects. This section covers more real-world use cases that Gradle users come across. Some examples include migrating to Gradle from the existing build system, using Gradle on CI servers, maintaining code quality with Gradle, using Gradle to build project languages such as Groovy and Scala, and so on. These concepts mostly revolve around what various plugins have to offer and also allows the reader to create their own custom plugins.

Also, there are multiple places in all chapters where the reader can find tips, references, and other informative notes.

Chapter 1, Running Your First Gradle Task, starts with an introduction to Gradle and its installation, subsequently moving on to exploring the Gradle command-line interface, and finally running the first build file.

Chapter 2, Building Java Projects, explains topics such as building Java applications and libraries, unit testing with JUnit, reading test reports, and creating application distributions.

Chapter 3, Building a Web Application, deals with building and running Web applications. It also briefly introduces concepts such as dependencies, repositories, and configurations.

Chapter 4, Demystifying Build Scripts, starts with a primer to the Groovy syntax in the context of Gradle DSL. Then, it goes on to explain the backbone concepts of a Gradle build such as build phases, project API, and various topics related to Gradle tasks.

Chapter 5, Multiprojects Build, covers a few options to structure multiproject directories. Then, covers organization of a build logic, which is a multiproject build.

Chapter 6, The Real-world Project with Gradle, deals with one of the important problems faced by developers, that is, migrating their existing Ant and Maven scripts to Gradle. This chapter provides different strategies and examples, which guide developers to perform migration in a more simpler and manageable way. This chapter also gives an insight into the different ways of publishing artifacts with the help of Gradle and also how a developer can integrate Gradle with Continuous Integration workflow.

Chapter 7, Testing and Reporting with Gradle, deals with the integration of the TestNG framework with Gradle. Apart from unit testing with TestNG, it also deals with different strategies for integration testing, which the user can follow to execute integration tests separate from unit test cases. It also discusses about integrating Sonar with Gradle, which helps developers to analyze the quality of code on different parameters, and JaCoCo integration for code coverage analysis.

Chapter 8, Organizing Build Logic and Plugins, discusses one of the important building blocks of Gradle plugins, without which you will find this book incomplete. It discusses the needs of the plugin and the different ways in which developers can create a plugin based on the project size and complexities.

Chapter 9, Polyglot Projects, demonstrates how to use Gradle for projects that use languages apart from or in addition to Java; this chapter shows the examples of building Groovy and Scala projects.

What you need for this book

Your system must have the following software before executing the code mentioned in the book:

  • Gradle

  • Java 1.7 or above

For chapters 6-8, you need the following softwares:

  • Jenkins

  • Ant 1.9.4

  • Maven 3.2.2

Who this book is for

This book is for Java and other JVM-based language developers who want to use Gradle or who 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.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "This class exposes just one method called greet which we can use to generate a greeting message."

A block of code is set as follows:

task helloWorld << {
  println "Hello, World!"
} 

Any command-line input or output is written as follows:

$ gradle --version

Or it may be written as follows:

> gradle --version

Whenever some output or code block is truncated it is denoted by an ellipsis (...) like this:

$ gradle tasks
...
Other tasks
-----------
helloWorld
...

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Once the Submit button is pressed, we'll get the desired result."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.