Gradle Automation

What is Gradle ?

Gradle is an open-source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration. Gradle uses a directed acyclic graph ("DAG") to determine the order in which tasks can be run.

Gradle was designed for multi-project builds, which can grow to be quite large. It supports incremental builds by intelligently determining which parts of the build tree are up to date; any task dependent only on those parts does not need to be re-executed.

The initial plugins are primarily focused on Java, Groovy and Scala development and deployment, but more languages and project workflows are on the roadmap.

Source: Wikipedia


Advantages of Gradle

Clear syntax

Opposite to its ancestors Ant and Maven, the syntax of Gradle is clear and easy to read. It's close to Groovy because of its Groovy-based DSL.

task sayHello {
  doLast {
    println "Hi there !"
  }
}

Running this task from the command line is simple as well.

gradle sayHello

Taking another example: copying files, including sub-directories.

task copyDocs(type: Copy) {
    from 'src/main/doc'
    into 'build/target/doc'
}

Compare that to Ant and Maven !


Extendable with Groovy

Although a lot of plugins exist, typically for Java and Groovy development, you can always extend Gradle with your own functions written in Groovy, either inside the Gradle code, or via plugins.

For example, the code below encourages your developers to work in a pre-defined directory structure.

["src/main/jbake", "src/main/shell", "src/test"].each { dirName ->
  new File(dirName).mkdirs()
}

The below function is used to validate a directory containing .html files. These files are generated by JBake and unfortunately, JBake does not close the <br> and <hr> tags when it writes the html code based upon Markdown. This problem can easily be solved with a little function that can be called in a task. Notice also that you should be fine with the syntax if you know Java or Groovy.

task correct {
  doLast {
    correctHtmlDir new File('build/output')
  }
}

def correctHtmlDir(File directory) {
  directory.eachFile {file ->
    if (file.name.endsWith(".html")) {
      def text = file.text

      text = text.replaceAll("<br>", "<br/>")
      text = text.replaceAll("<hr>", "<hr/>")

      file.delete()
      file << text
    }
  }
}

Java and Groovy developers feel at home

Developers familiar with Java and/or Groovy feel at home in Gradle immediately as the syntax is familiar. No need to learn yet another language or DSL.

And as they feel at home, most likely with some extra help in the beginning, the developers can maintain the Gradle solution themselves without needing extra resources.


All Java libraries can be included

As Gradle was originally meant for Java and Groovy developers, it was crucial that they could easily integrate Java libraries and maintain them when newer versions were released. Gradle's developers therefore included both MavenCentral and JCenter in their standard repositories for libraries and with just a couple of lines of code, you can download the libraries you need in a project.

import org.apache.commons.codec.binary.Base64

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
    }
}

task encode {
    doLast {
        def byte[] encodedString = new Base64().encode('hello world\n'.getBytes())
        println new String(encodedString)
    }
}

This technique can be used to download your own applications onto a server for deployment. In the lines above, replace mavenCentral with your Nexus server and replace the dependency with your application's version and you are all set to go.


How can Asynchrone help with Gradle ?

Setting up your Gradle projects

If you are not familiar with Gradle, but you want to switch from Ant or Maven to Gradle, Asynchrone can assist you in setting up your project:

  • Write the build files
  • Teach your developers how to work with Gradle
  • Teach your team how to maintain and extend the Gradle solution

Coding extra features

If the out-of-the-box Gradle solution is not doing all it should, Asynchrone can write the extra steps it takes.

View the actual build.gradle file used for building this website with JBake. These about 400 lines of code are building the website you are currently visiting, including optimalization of the content, validations and more.


Coding plugins for Gradle

In some cases, coding everything straight into the build.gradle file is not what you want. If you share this code with multiple projects, the maintenance becomes a nightmare. In such a case, you should create a Gradle plugin, which is like a library you can download. This plugin can be easily distributed to all your teams.

Asynchrone is currently working on the first plugins it will release in open source or as a commercial package. The investment was done to buy the domain gradleplugins.com. Check back soon when Asynchrone goes live with those plugins.


More information


Coming soon

https://www.gradleplugins.com

Page last updated on: 2018-09-10