Jim Cheung

Gradle in Action

(reading notes)

Chapter 1 Introduction to project automation

(no notes)

Chapter 2 Next-generation builds with Gradle

gradle environment variables:

only ouput task's output (quite)

gradle -q task-name

simple task:

task hello {
    doLast {
        println 'hello'
    }
}

the left shift operator << is a shortcut for the action doLast

task hello << {
    println 'hello'
}

keyword dependsOn indicates dependencies between tasks. dependsOn is actually a method of task.

task2.dependsOn task1
task task3(dependsOn: task2)

use --all to see the execution order of a task graph:

gradle -q tasks --all

you can execute multiple tasks in a single build run:

gradle task1 task2

tasks are always executed just once, no matter whether they're specified on the command line or act as a dependency for another task.

you can use abbreviate camel-cased task names:

task groupTherapy << { ... }
gradle gT

to exclude a specific task from build run, use -x

gradle gT -x task1

gradle excluded the task task1 and its dependent tasks

gradle daemon

for continues build, use gradle daemon

gradle gT --daemon

the daemon process will only be forked once even though you add the --daemon

daemon process will automatically expire after a 3-hour idle time.

to build without the daemon, use --no-daemon

to stop the daemon process, run gralde --stop

Chapter 3 Building a Gradle project by example

using the java plugin

apply plugin: 'java'

by default, the source layout is like:

(note, for automatic project generation, the book doesn't mention the Build Init Plugin, you can run gradle init --type java-library to generate a default project layout)

just simply run gradle build

on the output, message UP-TO-DATE means the task was skipped.

to run:

java -cp build/classes/main com.foo.Bar

customizing

gradle properties gives you a list of configurable standard and plugin properties, plus their default values. you can customize them by extending the build script.

version = 0.1
sourceCompatibility = 1.6
jar {
    manifest {
        attributes 'Main-Class': 'com.foo.Bar'
    }
}

then you can run the app with:

java -jar build/libs/foo-0.1.jar

example of changing project default layout,

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
    test {
        java {
            srcDirs = ['test']
        }
    }
}
buildDir = 'out'

Using external dependencies

add dependencies:

repositories {
    mavenCentral()
}
dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
}

(note, another common repository is jcenter(), web search I use http://mvnrepository.com/, the above line can also written as compile 'org.apache.commons:commons-lang3:3.1')

the war plugin

apply plugin: 'war'

default directory for web application sources is src/main/webapp

servlet dependencies:

dependencies {
    providedCompile 'javax.servlet:servlet-api:2.5'
    runtime 'javax-servlet:jstl:1.1.2'
}

dependencies marked provided aren't going to be packaged with the WAR file. On the other hand, runtime dependencies will be packaged.

customizing the WAR plugin:

warAppDirName = 'webfiles'
war {
    from 'static'
}

the jetty plugin

apply plugin: 'jetty'

then run gradle jettyRun

the jetty plugin allows you to change static resources and JSP files on the fly without having to restart the container.

customizing the jetty plugin:

jettyRun {
    httpPort = 9090
    contextPath = 'app'
}

Gradle wrapper

the wrapper is a core feature and enables a machine to run a Gradle build script without having to install the runtime. It also ensures that the build script is run with a specific version of Gradle.

Using the wrapper is considered best practice and should be mandatory for every Gradle project.

setup wrapper

task wrapper(type: Wrapper) {
    gradleVersion = '1.7'
}

then run: gradle wrapper

keep in mind that you'll only need to run gradle wrapper on your project once.

using the wrapper

gradlew.bat jettyRun

Chapter 4 Build script essentials

Building blocks
Working with tasks
Hooking into the build lifecycle
Summary

Chapter 5 Dependency management

A quick overview of dependency management
Learning dependency management by example
Dependency configurations
Declaring dependencies
Using and configuring repositories
Understanding the local dependency cache
Troubleshooting dependency problems
Summary

Chapter 6 Multiproject builds

Modularizing a project
Assembling a multiproject build
Configuring subprojects
Individual project files
Customizing projects
Summary

Chapter 7 Testing with Gradle

Automated testing
Testing Java applications
Unit testing
Configuring test execution
Integration testing
Functional testing
Summary

Chapter 8 Extending Gradle

Introducing the plugin case study
From zero to plugin
Writing a script plugin
Writing custom task classes
Using and building object plugins
Summary

Chapter 9 Integration and migration

Ant and Gradle
Maven and Gradle
Comparing builds
Summary

Chapter 10 IDE support and tooling

Using IDE plugins to generate project files
Managing Gradle projects in popular IDEs
Embedding Gradle with the tooling API
Summary

Chapter 11 Building polyglot projects

Managing JavaScript with Gradle
Building polyglot, JVM-based projects
Other languages
Summary

Chapter 12 Code quality management and monitoring

Integrating code analysis into your build
Measuring code coverage
Performing static code analysis
Integrating with Sonar
Summary

Chapter 13 Continuous integration

Benefits of continuous integration
Setting up Git
Building a project with Jenkins
Exploring cloud-based solutions
Modeling a build pipeline with Jenkins
Summary

Chapter 14 Artifact assembly and publishing

Building artifacts and distributions
Publishing artifacts to a binary repository
Publishing to a public binary repository
Artifact assembly and publishing as part of the build pipeline
Summary

Chapter 15 Infrastructure provisioning and deployment

Infrastructure provisioning
Targeting a deployment environment
Automated deployments
Deployment tests
Deployment as part of the build pipeline
Summary