Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gradle Goodness: Methods Generated for Setting Task Properties

DZone's Guide to

Gradle Goodness: Methods Generated for Setting Task Properties

If we create our own tasks in Gradle, we usually extend from the DefaultTask or a subclass of DefaultTask. This is different from the setProperty and getProperty methods already added by Groovy, but how? Let's look at the details.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

If we create our own tasks in Gradle, we usually extend from the DefaultTask or a subclass of DefaultTask. The tasks that are included with Gradle also extend from this class. Gradle will create a proxy class for the actual class implementation and adds (among other things) also a property setter method. The method has the name of the property and has a single argument of the same type as the property. It is different from the setProperty and getProperty methods already added by Groovy. For example, if we have a task with a property with the name message of type String then Gradle will add the method message(String) to the proxy class.

In the following example task, we have one property, user:

// File: buildSrc/src/main/groovy/com/mrhaki/gradle/Hello.groovy
package com.mrhaki.gradle

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class Hello extends DefaultTask {
    String user

    @TaskAction
    void sayHello() {
        println "Hello $user, how are you doing?"
    }
}

With the following test, we can check that the method user(String) is actually created and we can use it to set a value for the user property:

// File: src/test/groovy/com/mrhaki/gradle/HelloTaskSpec.groovy
package com.mrhaki.gradle

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
import spock.lang.Subject

class HelloTaskSpec extends Specification {

    @Subject
    private Hello task

    private Project project = ProjectBuilder.builder().build()

    def "user() method is generated to set user property"() {
        when:
        task = project.task('helloSample', type: Hello) {
            // Gradle generates proxy class to allow
            // 'setter(value)' methods for properties.
            // In this case we invoke user(String),
            // which maps to setUser(String).
            user 'sample'
        }

        then:
        task.user == 'sample'
    }

    def "Groovy syntax property assignment to set user property"() {
        when:
        task = project.task('helloSample', type: Hello) {
            // Groovy syntax for setUser(String).
            user = 'sample user'
        }

        then:
        task.user == 'sample user'
    }
}

The following build file has two tasks of type Hello where we use the user(String) method and a property assignment:

import com.mrhaki.gradle.Hello

task helloMrhaki(type: Hello) {
    user = 'mrhaki'
}

task helloHubert(type: Hello) {
    user 'Hubert'
}

The idea of this post came from watching a great presentation by Vitaliy Zasadnyy about Gradle plugins during a Mashup Gradle/Android Meetup.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
gradle

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}