Blog

Learning Gradle – An Open Source Build Automation Tool

01 Dec, 2018
Xebia Background Header Wave

Gradle is an open-source build automation tool focused on flexibility and performance. Gradle build scripts are written using a Groovy or Kotlin DSL. The focus of Gradle is to build your software. Grade needs Java to operate. Gradle was released in 2007, has a great track record, is battle proven and is a stable build tool. Gradle is widely used in the industry for automating the creation of software. Gradle comes with great documentation including several tutorials and a YouTube channel containing tutorials and talks.
Gradle is highly extensible. By means of plugins and tasks, Gradle can be extended to execute all kinds of tasks like build source code, generate documentation, create artifacts, generate code, and more. Gradle can build Java, Groovy, Scala, Python, and native languages like C and C++. There are a lot of community plugins that allow Grade to do a lot more. Lets find out how to use this useful tool.

Some assumptions

Before we move on, I will assume that you have a Mac, setup with homebrew and have experiences with JVM languages like Java or Kotlin. Gradle is highly flexible and supports several languages for DSL, but we will be using the Kotlin DSL.

Installation

On a Mac, gradle can be easily installed with:

$ brew install gradle
$ gradle --version

Welcome to Gradle 5.0!

Here are the highlights of this release:
 - Kotlin DSL 1.0
 - Task timeouts
 - Dependency alignment aka BOM support
 - Interactive `gradle init`

For more details see https://docs.gradle.org/5.0/release-notes.html


------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------

Build time:   2018-11-26 11:48:43 UTC
Revision:     7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987

Kotlin DSL:   1.0.4
Kotlin:       1.3.10
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_172 (Oracle Corporation 25.172-b11)
OS:           Mac OS X 10.14.1 x86_64

Create a Java Application

Lets create a new project that will build a Java application.

$ pwd
~/projects

$ mkdir helloworld-java
$ cd helloworld-java 

To create a new project, we need the init task that can be configured. Lets look at the help of this task.

$ gradle help --task :init
Options
     --dsl     Set the build script DSL to be used in generated scripts.
     --package     Set the package for source files.
     --project-name     Set the project name.
     --test-framework     Set the test framework to be used.
     --type     Set the type of project to generate.
                Available values are:
                     java-application
                     java-library
                     kotlin-application
                     kotlin-library
                     scala-library

To create a java-application project, type the following:

$ gradle init \
    --type java-application \
    --dsl kotlin \
    --test-framework junit \
    --package com.github.binxio.helloworld \
    --project-name helloworld-java

The following files and directories is created:

.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── github
    │   │           └── binxio
    │   │               └── helloworld
    │   │                   └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── com
        │       └── github
        │           └── binxio
        │               └── helloworld
        │                   └── AppTest.java
        └── resources

Basic Workflow

Gradle has a basic workflow like creating a new project, testing code, running code, and building the project to an artifact.

# clean the project
$ gradle clean
1 actionable task: 1 executed

# run the tests
$ gradle test
3 actionable tasks: 3 executed

# run the application
$ gradle run 
Hello world.

# create a distribution zip
$ gradle distZip
4 actionable tasks: 4 executed

# view the contents of the zip
$ unzip -l ./build/distributions/helloworld-java.zip
Archive:  ./build/distributions/helloworld-java.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-02-2018 15:39   helloworld-java/
        0  12-02-2018 15:39   helloworld-java/lib/
     1259  12-02-2018 15:39   helloworld-java/lib/helloworld-java.jar
  2740721  12-02-2018 15:05   helloworld-java/lib/guava-26.0-jre.jar
    19936  12-02-2018 15:05   helloworld-java/lib/jsr305-3.0.2.jar
   193322  12-02-2018 15:05   helloworld-java/lib/checker-qual-2.5.2.jar
    13704  12-02-2018 15:05   helloworld-java/lib/error_prone_annotations-2.1.3.jar
     8764  12-02-2018 15:05   helloworld-java/lib/j2objc-annotations-1.1.jar
     3482  12-02-2018 15:05   helloworld-java/lib/animal-sniffer-annotations-1.14.jar
        0  12-02-2018 15:39   helloworld-java/bin/
     2510  12-02-2018 15:39   helloworld-java/bin/helloworld-java.bat
     5518  12-02-2018 15:39   helloworld-java/bin/helloworld-java
---------                     -------
  2989216                     12 files

Tasks

When a project has been created with the init task, a number of plugins have been configured. These plugins add tasks to Grade by which it can execute common tasks that are necessary to successfully build software. To list which tasks are available type gradle tasks. A number of tasks will be shown that are applicable.

$ gradle tasks
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Project Configuration

A Gradle project is defined by two files, build.gradle.kts that defines the project, dependencies, tasks and settings.gradle.kts that defines the settings by means of key-value pairs.

build.gradle.kts:

plugins {
    // Apply the java plugin to add support for Java
    java

    // Apply the application plugin to add support for building an application
    application
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    implementation("com.google.guava:guava:26.0-jre")

    // Use JUnit test framework
    testImplementation("junit:junit:4.12")
}

application {
    // Define the main class for the application
    mainClassName = "com.github.binxio.helloworld.App"
}

settings.gradle.kts:

rootProject.name = "helloworld-java"

Create a Java Library

To create a java-library type:

$ pwd
~/projects

# create a new project dir
$ mkdir helloworld-lib
$ cd helloworld-lib

# initialize a project
$ gradle init \
    --type java-library \
    --dsl kotlin \
    --test-framework junit \
    --package com.github.binxio.helloworld \
    --project-name helloworld-lib

# run the test    
$ gradle test
3 actionable tasks: 2 executed, 1 up-to-date

# create a jar
$ gradle assemble

# view the contents of the jar
unzip -l build/libs/helloworld-lib.jar       
Archive:  build/libs/helloworld-lib.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-02-2018 15:48   META-INF/
       25  12-02-2018 15:48   META-INF/MANIFEST.MF
        0  12-02-2018 15:48   com/
        0  12-02-2018 15:48   com/github/
        0  12-02-2018 15:48   com/github/binxio/
        0  12-02-2018 15:48   com/github/binxio/helloworld/
      388  12-02-2018 15:48   com/github/binxio/helloworld/Library.class
---------                     -------
      413                     7 files

Create a Kotlin Application

To create a kotlin-application type:

$ pwd
~/projects

# create a new project dir
$ mkdir helloworld-kotlin
$ cd helloworld-kotlin

# initialize a project
$ gradle init \
    --type kotlin-application \
    --dsl kotlin \
    --test-framework kotlintest \
    --package com.github.binxio.helloworld \
    --project-name helloworld-kotlin

# run the test    
$ gradle test
5 actionable tasks: 5 executed

# create a jar
$ gradle run
Hello world.

$ gradle distZip
4 actionable tasks: 4 executed

# view the contents of the zip
$ unzip -l ./build/distributions/helloworld-kotlin.zip
Archive:  ./build/distributions/helloworld-kotlin.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-02-2018 15:55   helloworld-kotlin/
        0  12-02-2018 15:55   helloworld-kotlin/lib/
     2410  12-02-2018 15:55   helloworld-kotlin/lib/helloworld-kotlin.jar
    13769  12-02-2018 15:53   helloworld-kotlin/lib/kotlin-stdlib-jdk8-1.3.10.jar
     3137  12-02-2018 15:53   helloworld-kotlin/lib/kotlin-stdlib-jdk7-1.3.10.jar
  1181292  12-02-2018 15:53   helloworld-kotlin/lib/kotlin-stdlib-1.3.10.jar
   151024  12-02-2018 15:53   helloworld-kotlin/lib/kotlin-stdlib-common-1.3.10.jar
    17536  12-02-2018 15:53   helloworld-kotlin/lib/annotations-13.0.jar
        0  12-02-2018 15:55   helloworld-kotlin/bin/
     2493  12-02-2018 15:55   helloworld-kotlin/bin/helloworld-kotlin.bat
     5498  12-02-2018 15:55   helloworld-kotlin/bin/helloworld-kotlin
---------                     -------
  1377159                     11 files

Create a Kotlin Library

To create a kotlin-library type:

$ pwd
~/projects

# create a new project dir
$ mkdir helloworld-kotlin-library
$ cd helloworld-kotlin-library

# initialize a project
$ gradle init \
    --type kotlin-application \
    --dsl kotlin \
    --test-framework kotlintest \
    --package com.github.binxio.helloworld \
    --project-name helloworld-kotlin-library

# run the test    
$ gradle test
5 actionable tasks: 5 executed

# create a jar
$ gradle assemble
2 actionable tasks: 1 executed, 1 up-to-date

# view the contents of the jar
$ unzip -l build/libs/helloworld-lib.jar       
unzip -l build/libs/helloworld-kotlin-library.jar
Archive:  build/libs/helloworld-kotlin-library.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-02-2018 15:58   META-INF/
       25  12-02-2018 15:58   META-INF/MANIFEST.MF
       55  12-02-2018 15:58   META-INF/helloworld-kotlin-library.kotlin_module
        0  12-02-2018 15:58   com/
        0  12-02-2018 15:58   com/github/
        0  12-02-2018 15:58   com/github/binxio/
        0  12-02-2018 15:58   com/github/binxio/helloworld/
     1146  12-02-2018 15:58   com/github/binxio/helloworld/AppKt.class
      766  12-02-2018 15:58   com/github/binxio/helloworld/App.class
---------                     -------
     1992                     9 files

Create a Scala Library

To create a scala-library type:

$ pwd
~/projects

# create a new project dir
$ mkdir helloworld-scala-library
$ cd helloworld-scala-library

# initialize a project
$ gradle init \
    --type scala-library \
    --dsl kotlin \
    --test-framework scalatest \
    --package com.github.binxio.helloworld \
    --project-name helloworld-scala-library

# run the test    
$ gradle test
5 actionable tasks: 5 executed

# create a jar
$ gradle assemble
2 actionable tasks: 1 executed, 1 up-to-date

# view the contents of the jar
$ unzip -l build/libs/helloworld-scala-library.jar                                                 ✔  985  16:03:42
Archive:  build/libs/helloworld-scala-library.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-02-2018 16:03   META-INF/
       25  12-02-2018 16:03   META-INF/MANIFEST.MF
        0  12-02-2018 16:03   com/
        0  12-02-2018 16:03   com/github/
        0  12-02-2018 16:03   com/github/binxio/
        0  12-02-2018 16:03   com/github/binxio/helloworld/
      737  12-02-2018 16:03   com/github/binxio/helloworld/Library.class
---------                     -------
      762                     7 files

Conclusion

Grade is a build automation tool that can build most programming language. What is great about Gradle is that it uses the same plugins and configuration for each application. In this project we have created a Java, Kotlin and Scala project, ran tests and created build artifacts. Kotlin is the build tool of choice when you are creating Kotlin projects.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts