Properties of DefaultTask in Gradle

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.

 

didWork

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:

 

 

enabled

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.

 

path

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.

 

Logger and logging

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

 

description

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'

 

temporaryDir

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.

 

Dynamic Properties

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.

 

References

http://www.gradle.org

http://www.gradle.org/docs/current/userguide/logging.html

Book: Building and Testing with Gradle by Tim Berglund and Matthew McCullough.

 

Software Versions Tested Against

Examples in this note has been tested against Gradle versions 1.11 and 2.1.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)