Groovy-DSL does not generate setter for Property-typed properties

According to the userguide’s Lazy Configuration section:

Note that Groovy Gradle DSL will generate setter methods for each Property -typed property in a task implementation. These setter methods allow you to configure the property using the assignment ( = ) operator as a convenience.

This appears to not function when the task class’s field is explicitly public.

class PropTest extends DefaultTask {
    @Input
    final Property<String> message = project.objects.property( String )
    /* does not work (Cannot cast object 'hi' with class 'java.lang.String' to class 'org.gradle.api.provider.Property')
    public final Property<String> message = project.objects.property( String )
    */

    @TaskAction
    void doWork() {
        println message.get()
    }
}
task hi( type: PropTest ) {
    message = "hi"
}

I noticed this when I tried porting a task class from the build script into a java class in a plugin. Is this a bug or is there missing documentation about the limitations?

1 Like

Any update on this? I’m using IntelliJ, and it’s showing warnings about the above issue, despite the code working fine when run.

When I make the property-type field package-private or private, I get the following warning:
"Cannot assign 'Boolean' to 'Property<Boolean>'. Access to '...' exceeds its access rights"

I think I finally understand what was going on. I just learned that specifying public on a field in Groovy is !!NOT!! the same as excluding the access modifier. Without the access modifier you get a property, which is a getter/setter backed by a private field, whereas specifying public results in a public field and no getter/setter.
This probably explains a number of weird field/property related things I’ve seen over the years where I’ve given up and found alternative solutions. #theMoreYouKnow

1 Like