DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Gradle Goodness: Methods Generated for Setting Task Properties

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.

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Mar. 14, 16 · DevOps Zone · Tutorial
Like (4)
Save
Tweet
1.78K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Property (programming) Task (computing) 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.

Popular on DZone

  • The 5 Healthcare AI Trends Technologists Need to Know
  • Growth in Java Development for Web and Mobile Apps
  • 10 Books Every Senior Engineer Should Read
  • 6 Best Books to Learn Multithreading and Concurrency in Java

Comments

DevOps Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo