Book Image

Gradle Effective Implementations Guide - Second Edition

By : Hubert Klein Ikkink
Book Image

Gradle Effective Implementations Guide - Second Edition

By: Hubert Klein Ikkink

Overview of this book

Gradle is a project automation tool that has a wide range of applications. The basic aim of Gradle is to automate a wide variety of tasks performed by software developers, including compiling computer source code to binary code, packaging binary codes, running tests, deploying applications to production systems, and creating documentation. The book will start with the fundamentals of Gradle and introduce you to the tools that will be used in further chapters. You will learn to create and work with Gradle scripts and then see how to use Gradle to build your Java Projects. While building Java application, you will find out about other important topics such as dependency management, publishing artifacts, and integrating the application with other JVM languages such as Scala and Groovy. By the end of this book, you will be able to use Gradle in your daily development. Writing tasks, applying plugins, and creating build logic will be your second nature.
Table of Contents (18 chapters)
Gradle Effective Implementations Guide - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Skipping tasks


Sometimes, we want tasks to be excluded from a build. In certain circumstances, we just want to skip a task and continue executing other tasks. We can use several methods to skip tasks in Gradle.

Using onlyIf predicates

Every task has an onlyIf method that accepts a closure as an argument. The result of the closure must be true or false. If the task must be skipped, the result of the closure must be false, otherwise the task is executed. The task object is passed as a parameter to the closure. Gradle evaluates the closure just before the task is executed.

The following build file will skip the longrunning task, if the file is executed during weekdays, but will execute it during the weekend:

import static java.util.Calendar.* 
 
task longrunning { 
    // Only run this task if the 
    // closure returns true. 
    onlyIf { task -> 
        def now = Calendar.instance 
        def weekDay = now[DAY_OF_WEEK] 
        def weekDayInWeekend = weekDay in [SATURDAY, SUNDAY] 
    ...