Wed 15 Mar 2017

Gradle

A Java build tool.

We use this for the National Household Model (NHM).

Gradle claims to integrate with everything and to do it at runtime.

It uses Groovy, and comes with it built in.

Gradle CLI

Looks for build.gradle in the current directory, or you can specify -b somefile.

gradle compile test executes the compile and test tasks

--continue flag builds everything it can. Otherwise, it dies on the first error.

--rerun-tasks forces everything to build. Otherwise, it looks a timestamps to decide what needs updating.

gradle help --task sometask gives usage information.

-m does a dry run.

--continuous does a continuous build:

  • Continuous builds watch the input files for changes.
  • Continuous build watching does not follow symlinks.
  • Continuous build watching ignores files describing the build itself.

Gradle Wrapper

Downloads the right version of Gradle, then runs it.

Put the Gradle wrapper in your project by running the wrapper task.

Gradle Daemon

Runs in the background to speed things up.

It's enabled by default these days.

Run ./gradlew --stop to kill them.

Use the --no-daemon argument to prevent their use.

They do some caching, which may break correctness? Seems unlikely unless you're really messing around though?

Tasks

Gradle sorts out the dependency tree and runs each task at most once.

task thing(dependsOn: [a, b]) {
     doLast {
            println "hmm hmm"
     }
}

Tasks are visible if they have been assigned to a group.

Builtin

The projects task prints a tree of projects and sub-projects (not a dependency graph).

The tasks task lists all the tasks which are visible.

The properties task lists properties.

The dependencies task prints the dependency tree.

The dependencyInsight task tells you why a particular dependency (and version) was selected.

Groovy

About variable scope:

String localScope1 = "blah"
def localScope2 = "blah blah"
scriptScope = "blah everywhere"

Local scoped variables won't show up in functions you define in the same scope.

They can be closed over in {} blocks.

build.gradle

apply plugin: 'java'

// Where we get inputs from
repositories {
  mavenCentral()
  maven {
    url "some-location"
  }
}

// Which inputs we need
dependencies {
  compile dependency_group: 'org.cse.uk.blah', name: 'blah', version: '1.0.0'
  compile dependency_group: 'org.cse.uk.blah: 1.0.0' // The short version
}

apply plugin: 'maven'

// Where our outputs go
uploadArtifacts {
  repositories {
    mavenDeployer {
      repository(url: 'some-location')
    }
  }
}

settings.gradle

This lists which projects are included in a multiproject build.

To run tasks, use their qualified name (: instead of /), e.g. :project:sub-project:task.

gradle.properties

This file is for setting machine-specific or user-specific variables.

You could also put these in at the command line instead.

Composite Build

A build which triggers another build.

These are probably less useful than multi-project builds.

Java Plugin

Defines dependency groups:

  • compile
  • runtime (includes compile dependencies)
  • testCompile (includes compile dependencies, compile outputs)
  • testRuntime (includes compile, runtime, testCompile dependencies)

Expects the usual src/main/java and src/main/resources conventions.

Outputs go into build, JARs go into build/libs.

sourceCompatability defines the Java version.

Eclipse Plugin

Lets you make .project files.

Maven Plugin

Defines the install task, to upload your outputs.

Offers a factory method pom to output a POM file.

Knows to read your Maven configuration settings.xml.

Maven requires 1 POM per artifact. Gradle lets you define a project with multiple artifacts, and output multiple POMs for it.