Engineering Full Stack Apps with Java and JavaScript
We will download, install and configure gradle, and then execute a simple program.
Gradle 1.x requires a Java JDK 5 or higher to be installed and the location needs to be added to an environment variable JAVA_HOME.
Gradle 2.x requires a Java JDK 6 or higher to be installed and the location needs to be added to an environment variable JAVA_HOME.
Note: Gradle ships with its own Groovy library, therefore no Groovy needs to be installed. Any existing Groovy installation is ignored by Gradle.
You can download gradle from http://www.gradle.org/downloads. Download binaries-sources-documentation version or binaries version.
Unzip the zip to a location (e.g. D:\gradle-1.11)
Now add the gradle location (e.g. D:\gradle-1.11) to an environment variable called GRADLE_HOME and add GRADLE_HOME\bin (enter full path) to PATH environmental variable.
Now open a command prompt and execute gradle to verify if it is there in the PATH.
We will verify gradle setup by printing a “Hello World” from the build file.
Createa simple build file named ‘build.gradle’:
task helloWorld << {
println 'Hello World'
}
Now run this as ‘gradle <taskname>’:
gradle helloWorld
This will print:
:helloWorld
Hello World
BUILD SUCCESSFUL
Total time: 2.626 secs
You can use the –q option to suppress all info messages except error messages:
gradle -q helloWorld
This will print:
Hello World
Apart from the -q option we saw now, there are many other options. To see a list of all command line options, use the –h option as:
gradle -h
This will list out options. Below are some of them:
-Dproperty=value for defining a system property
--info or –i to set log level to INFO for more verbose output
--debug or –d to enable debug logs.
--dry-run or –m to dry run the build file without actually executing the actions.
--quiet or –q to suppress all info messages except error messages:
--gui to launches the Gradle GUI.
--stacktrace or –s to print an abbreviated stack trace when an exception is thrown by the build.
The -b can point Gradle to a nondefault build file. By default, it looks for a file called build.gradle.
You can find the documentation on standard Gradle features @ docs/dsl/index.html within the gradle installation.
Next we will see a Java Hello World program compiled using gradle.
http://www.gradle.org/downloads
http://www.gradle.org/installation
Book: Building and Testing with Gradle by Tim Berglund and Matthew McCullough.
Examples in this note has been tested against Gradle versions 1.11 and 2.1.