Gradle - bahkified/Notes GitHub Wiki
Reference for Gradle!
Getting started with Gradle
Gradle can be downloaded from the Gradle homepage. Once dowloaded, follow the install instructions on the site. A Gradle wrapper can be included with projects, eliminating the need for downloading Gradle on a given machine. The wrapper will automatically download Gradle if it is not already present.
The most basic Gradle build file will be called build.gradle
and will have one line: apply plugin: 'java'
. This plugin has all of the build tasks needed for a basic Java project. Other tasks can either be written explicitly or gotten through other plugins. For example, you can specifiy: apply plugin: 'war'
to add a task to package the built project into a WAR file.
Default tasks
You can set one or more default tasks that will be executed if no tasks are specified when the gradle script is run. The following will run the clean task, build task, and test task by default.
defaultTasks 'clean', 'build', 'test'
To run the default tasks, you just need to run gradle. That's it!
./gradlew
Since no tasks are defined in the gradle execution, it will run only the default tasks.
Source Sets
New source directories can be added to a gradle project using the sourceSets
closure. The following code will add the genSrc folder to the project tree and designate the 'build/generated-sources/folder' directory as a source folder.
sourceSets {
genSrc {
java {
srcDir 'build/generated-sources/folder'
}
}
}
Execute a command line tool in gradle
For whatever reason, you might want to use a command line tool from within the gradle build. In this case, several things need to be declared before the command line tool can successfully be executed. For this example, the Apache CXF tool, wsdl2java
, will be executed during a dedicated build task.
This linked code defines a task that executes the wsdl2Java command line tool provided by Apache CXF to generate client code for a SOAP based web service, defined in a WSDL file. When the defined task is executed, Java client code is generated and put into the directory specified by generatedWsdlDir
. This directory is also specified as a source directory, so that the generated code can be found and used.
task wsdl2Java {
if (!wsdlDir.listFiles()) {
// nothing to do
} else {
inputs.files wsdlDir.listFiles()
outputs.files generatedWsdlDir
doLast {
javaexec {
classpath configurations.cxfTools
main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
args = wsdlToGenerate
args.add(0, '-xjc-mark-generated')
args.add(0, '-client')
systemProperties = ['exitOnFinish' : 'TRUE']
}
}
}
}
The full build script with the dependencies and other variables defined can be found at Executing wsdl2Java in Gradle
Resources
Description | URL |
---|---|
Gradle homepage | http://www.gradle.org/ |