Skip to content
Shows how to create a reusable class in Gradle files
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Topera's Hello World #019

Reusing classes in Gradle files

This hello world shows how to reuse vars (global vars) and classes in Gradle files.

How to download this source code

  • Install Gradle
  • Use the Clone or download button above the get the url of this repository
  • In IntelliJ IDEA: File → New → Project From Version Control → Git → put the url of this repository

How to test

  • Run $ gradle showRed


  • We can split the main build.gradle file using a second file, like mySecondFile.gradle, using the apply from command
  • We created a global variable using the variable ext. in build.gradle file
  • File mySecondFile.gradle defines 2 custom tasks: showRed and showBlue.
    • Both are of type ShowColor, a custom class created by us in folder buildSrc/src/main/groovy/com/topera/gradle/ShowColor.groovy
  • When we run showRed the log displays:
      > Configure project :
      [COLORS] Configuring task red
      [COLORS] Configuring task blue
      > Task :showRed
      [COLORS] Running task red
      My color is red
  • There are 2 diff steps in Gradle. One for configuration, ran all the time: 'Configuring task red', 'Configuring task blue'. And another for the specific task called, in this case, red.

Folder Structure


Tech Stack

  • Intellij IDEA 2018.1
  • Gradle 4.8

To take a look in other projects, please see

You can’t perform that action at this time.