Gradle

From HaFrWiki
Jump to: navigation, search

Introduction

The default build tool for Android (and the new star of build tools on the JVM) is designed to ease scripting of complex, multi-language builds. Should you change to it, though, if you're using Ant or Maven? [1]

Build Tools

For years, builds had the simple requirements of compiling and packaging software. But the landscape of modern software development has changed, and so have the needs for build automation.

Today, projects involve large and diverse software stacks, incorporate multiple programming languages, and apply a broad spectrum of testing strategies. With the rise of agile practices, builds must support early integration of code as well as frequent and easy delivery to both test and production environments.

Established build tools regularly fall short in meeting these goals.

  • How many times have your eyes glazed over while looking at XML to figure out how a build works?
  • And why can't it be easier to add custom logic to your build?

All too often, when adding on to a build script, you can't shake the feeling that you're implementing a workaround or hack. I feel your pain.

There has to be a better way of doing these things in an expressive and maintainable way. There is — it's called Gradle.

What is it

Gradle benefits from

Gradle is the next evolutionary step in JVM-based build tools. It draws on lessons learned from established tools such as Ant and Maven and takes their best ideas to the next level.

Following a build-by-convention approach, Gradle allows for declaratively modeling your problem domain using a powerful and expressive domain-specific language (DSL) implemented in Groovy instead of XML.
Because Gradle is a JVM native, it allows you to write custom logic in the language you're most comfortable with, be it Java or Groovy.

Example Java Hello World

Follow the following steps:

  • Create a directory to put in the required files. From here assumed is that the name of this directory is /gradletest.
  • Go to the directory /gradletest.
  • Create the gradle build file build.gradle with content:
apply plugin: 'java'
  • Create a directory named: /gradletest/src/main/org/gradle/example/simple.
  • In the above directory, create a gradle file named build.gradle with the content:
package org.gradle.example.simple;

public class HelloWorld {
   public static void main(String args[]) {
      System.out.println("hello, world");
   }  // main
}  // class
  • In the directory /gradletest perform:
$ gradle build

The default name for the build file is build.gradle' and is therefor not needed on the command line.

  • The result of the build will be a directory structure as show below with left the user created directories and on the right the gradle generated structure.
User created structure Gradle expands this with:
gradletest
-- src
   -- main
       -- org
          -- gradle
             -- example
                -- simple
                   -- HelloWorld.java
-- build
   |- classes
   |  -- main
   |     -- org
   |         -- gradle
   |            -- example
   |                -- simple
   |                    -- HelloWorld.class
   |- dependency-cache
   |- libs
   |   -- minimal-java-build-file.jar
   |-reports
   |  -- tests
   |      |- css3-pie-1.Obeta3.htc
   |      |- index.html
   |      |- report.js
   |      |- style.css
   |- test-results
-- build.gradle
-- run-example.bsh
   |- src 
  etc. (See left side).

See also

top

Intrawiki

Reference

top

  1. DrDobbs, The heart of this article is a good-old Dr.Dobbs Journal Article. Back to the basics with Dr Dobbs!