Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Essential Groovy DSLs


[ 34 ]

If you already have Gradle installed with a different version you can switch versions
as follows:


$gvm use gradle 2.4


GVM takes care of all the detail of installing Gradle and setting up the path and
GRADLE_HOME environment for you. If you are not using the GVM tool, you can find
the Gradle installation packages at http://www.gradle.org/downloads. Then
configure the Gradle environment by following the instructions at http://www.
gradle.org/docs/current/userguide/installation.html.


Gradle basics


To understand Gradle builds, you need to only consider three basic
concepts initially:



  • The build script: Gradle automatically looks for a file called build.gradle
    in the current directory. This file is the build script, which defines the projects
    and tasks that make up the build.

  • Projects: Gradle can work with a single or multiple projects per build. For the
    purpose of this chapter, we will only be looking at single project builds.

  • Tasks: Gradle tasks are the building blocks of the build, for example,
    compile, test, and clean.


Gradle build scripts


Gradle automatically looks for a file called build.gradle in the current directory
and uses this as the build script. For most simple projects, all you ever need to write
is a build.gradle file. The source package that accompanies this book has a build.
gradle file in the root directory. This is a very simple build script file from which we
only care about one main task.


You can test all the code in the book with one Gradle command:


$gradle test


We will look at this build.gradle script in more detail later in the chapter to see
how this is achieved. But first, let's look at some basic Gradle tasks. In the following
examples, we will use build scripts with different names to build.gradle. This is
purely so that we can have more than one build script in the same directory. To run
a build script other than build.gradle from the command line, we will use the
following code:


$gradle -b hello.gradle


http://www.ebook3000.com
Free download pdf