0

I'm still new to Gradle and I'm struggling to understand how task configurations for plugin tasks work. Consider the Jar task from the Java plugin: if I overwrite the doFirst and doLast methods why does the plugin's original set of actions still occur but if I change the description of the Jar task then my new description is used? The contents of my build script are:

plugins{
    id 'java'
}

jar {
    description "The new description"
    doFirst {
        println "this happened first"
    }
    doLast {
        println "This happened last"
    }
}

I ask this more to solidify my understanding of how gradle works and not necessarily because I would like to do this yet.

1 Answer 1

0

A Gradle script is just a Groovy script (with some extras) that operates on the Gradle API written in Java. You can checkout the API documentation and most of your questions will be solved:

Consider the Jar task from the Java plugin

Let's be pedantic on this one: There is no Jar task from the Java plugin, there is a task named jar of type Jar. It is very important to distinguish between task types and task instances in this context. Now let's check out the documentation of the Jar task type in Gradle. As you can see, it inherits from the type DefaultTask. DefaultTask defines all those properties and methods that are common for Gradle tasks:

doFirst​(Closure action) - Adds the given closure to the beginning of this task's action list.
doLast​(Closure action) - Adds the given closure to the end of this task's action list.

As you can see, whenever you use doFirst or doLast, it will only append (or prepend) task actions to the list of existing task actions, nothing will be overriden.

Thanks to Groovy, assigning a value to the property description using = will actually call the method setDescription:

setDescription​(String description) - Sets a description for this task.

This is a classical property setter known from the Java world. It will simply set the new value and the old value will be overridden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.