Gradle Essentials

Book description

Master the fundamentals of Gradle using real-world projects with this quick and easy-to-read guide

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.

Table of contents

  1. Gradle Essentials
    1. Table of Contents
    2. Gradle Essentials
    3. Credits
    4. About the Authors
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Running Your First Gradle Task
      1. Installing Gradle
        1. Installing manually
          1. Installing on Mac OS X and Linux
          2. Installing on Windows
        2. Alternate methods of installing Gradle
          1. Installing via OS-specific package managers
            1. Mac OS X
            2. Linux (Ubuntu)
            3. Windows
          2. Installing via SDKMAN
        3. Verifying the installation
        4. Setting JVM options
      2. The Gradle command-line interface
      3. The first Gradle build script
        1. Task name abbreviation
        2. Gradle Daemon
        3. Gradle Wrapper
          1. Generating wrapper files
          2. Running a build via wrapper
      4. Summary
    10. 2. Building Java Projects
      1. Building a simple Java project
        1. Creating a build file
        2. Adding source files
        3. Building the project
        4. A brief introduction to plugins
      2. Unit testing
        1. Adding a unit test source
        2. Adding the JUnit to the classpath
        3. Running the test
        4. Viewing test reports
        5. Fitting tests in the workflow
      3. Bundling an application distributable
        1. Running the application with Gradle
        2. Building the distribution archive
      4. Generating IDE project files
      5. Summary
    11. 3. Building a Web Application
      1. Building a simple Java web project
        1. Creating source files
        2. Creating a build file
        3. Building the artifact
        4. Running the web application
          1. Plugins to the rescue
      2. References
      3. Project dependencies
        1. External libraries
          1. The dynamic version
          2. Transitive dependencies
        2. Dependency configurations
        3. Repositories
      4. Summary
    12. 4. Demystifying Build Scripts
      1. Groovy for Gradle build scripts
        1. Why Groovy?
        2. Groovy primer
          1. Running Groovy code
          2. Variables
            1. Strings
            2. Regular expressions
            3. Closures
          3. Data structures
            1. List
            2. Set
            3. Map
          4. Methods
            1. Calling methods
            2. Default values of parameters
            3. Methods with map parameters/named parameters
            4. Methods with varags
            5. Methods with closure params
          5. Classes
            1. Constructors
            2. Properties
            3. Instance methods
        3. Another look at applying plugins
      2. Gradle – an object-oriented build tool
      3. Build phases
        1. Initialization
        2. Configuration
        3. Execution
        4. Life cycle callbacks
      4. Gradle Project API
        1. Project methods
          1. Project properties
          2. Extra properties on a project
      5. Tasks
        1. Attaching actions to a task
        2. Task flow control
          1. dependsOn
          2. finalizedBy
          3. onlyIf
          4. mustRunAfter and shouldRunAfter
        3. Creating tasks dynamically
        4. Setting default tasks
        5. Task types
          1. Using task types
          2. Creating task types
      6. References
        1. Groovy
        2. Gradle API and DSL used in this chapter
      7. Summary
    13. 5. Multiprojects Build
      1. The multiproject directory layout
      2. The settings.gradle file
      3. Organizing build logic in multiproject builds
        1. Applying a build logic to all projects
        2. Applying build logic to subprojects
        3. Dependency on subprojects
      4. Summary
    14. 6. The Real-world Project with Gradle
      1. Migrating from an Ant-based project
        1. Importing an Ant file
        2. Using AntBuilder API
        3. Rewriting Ant tasks to Gradle tasks
      2. Migrating from a Maven project
      3. Publishing artifacts
      4. Continuous Integration
      5. Generating documentation
      6. Summary
    15. 7. Testing and Reporting with Gradle
      1. Testing with TestNG
      2. Integration testing
      3. Code coverage
      4. Code analysis reports
      5. Summary
    16. 8. Organizing Build Logic and Plugins
      1. Extracting build logic to buildSrc
      2. The first plugin
      3. Configuring plugins
      4. Summary
    17. 9. Polyglot Projects
      1. The polyglot application
      2. Building Groovy projects
      3. Building Scala projects
      4. Joint compilation
      5. References
      6. Summary
    18. Index

Product information

  • Title: Gradle Essentials
  • Author(s): Kunal Dabir, Abhinandan
  • Release date: December 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783982363