Engineering Full Stack Apps with Java and JavaScript
Gradle tasks are actually objects and a task object has properties and methods just like any other object. By default, each new task receives a type of DefaultTask in Gradle (similar to how all classes are children of the Object class in Java). DefaultTask only contain the functionality required for them to interface with the Gradle project model and do not do any additional actions such as compiling or executing code. We already saw the important methods of DefaultTask. Now we will see the important properties.
A boolean property indicating whether a task completed successfully.
Not all tasks may set this, but some built-in tasks like Compile, Copy, and Delete do set it.
Also, the exact meaning is task-specific. For example, the current Java Compiler returns didWork with true, if at least one file successfully compiled.
Example build file:
apply plugin: 'java'
task resultTask(dependsOn: compileJava) << {
if(tasks.compileJava.didWork) {
println 'COMPILE SUCCESS'
}
else{
println 'COMPILE NOT SUCCESS'
}
}
You need to place the java source code correctly as we have seen in the HelloWorld program. Also, make sure you delete any previous build folder. Gradle will only compile if there is a need. Else it won’t compile and hence won't set this property. So if you try to run again without deleting build folder, didWork will be false.
You can test it by running 'gradle resultTask' and you will get the output as below if there are no errors in your java program:
This Boolean property decides whether to execute a task or not. Even if the task is set as not enabled, its dependencies will still run. We will set the resultTask in previous example to false.
apply plugin: 'java'
task resultTask(dependsOn: compileJava) << {
if(tasks.compileJava.didWork) {
println 'COMPILE SUCCESS'
}
else{
println 'COMPILE NOT SUCCESS'
}
}
resultTask.enabled=false
When executed as ‘gradle resultTask’ you get output as:
:compileJava
:resultTask SKIPPED
BUILD SUCCESSFUL
Total time: 5.985 secs
Note that resultTask was skipped, but its dependency compileJava was executed.
Fully qualified path of a task.
task printPath << {
println "printPath PATH IS ${path}"
}
When executed as “gradle -q printPath”, it gives the output as:
printPath PATH IS :printPath
This will be very useful when you want to know the path of a subproject in case of a large build with subprojects or nested builds.
A reference to the internal Gradle logger object that implements the org.slf4j.Logger interface with few extra logging levels. Logging levels are DEBUG, INFO, LIFECYCLE, WARN, QUIET and ERROR.
The logging.level property allows us to access and change the logging level in use by the build.
task logTask << {
println "Current log level is ${logging.level}"
logger.debug('a debug message')
}
logTask.logging.level = 'DEBUG'
When executed as ‘gradle logTask’, this will give the output as:
19:50:07.194 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :logTask
19:50:07.218 [QUIET] [system.out] Current log level is DEBUG
19:50:07.293 [DEBUG] [org.gradle.api.Task] a debug message
BUILD SUCCESSFUL
Total time: 4.333 secs
An optional human-readable metadata to document the purpose of a task. Different ways to do so are:
task helloWorld(description: 'Says Hello World') << {
println 'Hello World' }
helloWorld { description = 'Says Hello World'}
helloWorld.description = 'Says Hello World'
Returns a File object pointing to a temporary directory belonging to this build file and can be used for to store intermediate results or to stage files.
You can also create properties dynamically. Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Example:
task dynaPropTask << {
myDynaProp = 'any string'
println '${myDynaProp}'
}
Try executing the below program with the gradle 1.11 as 'gradle dynaPropTask' and you will get the warning as:
:dynaPropTask
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.
org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "myDynaProp" on "task ':dynaPropTask'", value: "any string".
${myDynaProp}
BUILD SUCCESSFUL
Total time: 2.578 secs
Try executing the below program with the gradle 2.1 as 'gradle dynaPropTask' and you will get the error as:
:dynaPropTask FAILED
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\HJK-Files\LEARNING\Gradle\build.gradle' line: 3* What went wrong:
Execution failed for task ':dynaPropTask'.
> No such property: myDynaProp for class: org.gradle.api.DefaultTask_Decorated* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
http://www.gradle.org/docs/current/userguide/logging.html
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.