Tuesday, January 14, 2014

Learning Gradle

With Gradle, we can automate the compiling,
testing, packaging, and deployment of our software or other types of projects. Gradle
is flexible but has sensible defaults for most projects. This means we can rely on the
defaults, if we don't want something special, but can still use the flexibility to adapt
a build to certain custom needs.

Declarative builds and convention over configuration
Gradle uses a Domain Specific Language (DSL) based on Groovy to declare builds.
The DSL provides a flexible language that can be extended by us.

Support for Ant tasks and Maven repositories
Gradle wrapper
The Gradle wrapper allows us to execute Gradle builds, even though Gradle is not
installed on a computer.
Gradle
bundles the Groovy libraries with the distribution and will ignore a Groovy
installation already available on our computer.

A directory named init.d where we can store Gradle scripts that
need to be executed each time we run Gradle

gradle -v
If we want to add JVM options to Gradle, we can use the environment variables
JAVA_OPTS and GRADLE_OPTS.

build.gradle
task helloWorld << {
println 'Hello world.'
}
The << syntax is operator shorthand for the method leftShift(), e
want to add the closure to our task with the name helloWorld.

gradle helloWorld
--quiet (or -q)\
gradle --quiet helloWorld

Default Gradle tasks: gradle -q tasks

gradle -q help
gradle -q properties
The properties task is very useful to see the properties available to our project. 
gradle -q dependencies
gradle -q projects

Task name abbreviation
gradle -q hello
gradle -q hW

Executing multiple tasks: gradle helloWorld tasks

Command-line options
gradle --help
--build-file (or -b) and --project-dir (or -p).
gradle --project-dir hello-world -q helloWorld

Running tasks without execution
With the option --dry-run (or -m), we can run all the tasks without really executing
them.

Gradle daemon
We can reduce the build
execution time if we don't have to load a JVM, Gradle classes, and libraries, each time
we execute a build. The command-line option, --daemon, starts a new Java process
that will have all Gradle classes and libraries already loaded, and then we execute
the build. The next time when we run Gradle with the --daemon option, only the
build is executed, because the JVM, with the required Gradle classes and libraries,
is already running.
gradle --daemon helloWorld
gradle --no-daemon helloWorld
gradle --stop

alias gradled='gradle --daemon'
export GRADLE_OPTS="-Dorg.gradle.daemon=true"

Profiling: --profile
The data is saved in an HTML file in the directory build/reports/profile.

--gui: gradle --gui
project.description = 'Simple project'
setDescription("Simple project")
project.description project.getDescription()

We can use the doFirst and doLast methods to add actions to our task, and we can
use the left shift operator (<<) as a synonym for the doLast method. With the doLast
method or the left shift operator (<<) we add actions at the end of the list of actions
for the task. With the doFirst method we can add actions to the beginning of the
list of actions.

println "Running ${task.name}"

No comments:

Post a Comment