When to use `Property` for custom task?

The documentation for custom tasks just uses a private field and setter/getter combination for task properties. But the JavaExec task in gradle source code uses Property<String> for the mainClass task property (https://github.com/gradle/gradle/blob/f8d612ca6b1d21f79690800ceaf95c97b10ba0ac/subprojects/language-java/src/main/java/org/gradle/api/tasks/JavaExec.java#L114). What’s the advantage here? Should Property<T> be used for custom tasks?

1 Like

Property and Provider allow for lazy configuration whereby build authors can configure the source of the property before the actual value itself is known/configured.

You could support both styles of configuration with a convenience setter method

public class MyTask extends DefaultTask {
    private final Property<String> property = getProject().getObjects().property(String)

    public Property getProperty() {
        return this.property;

    public void setProperty(String value) {

    public void doStuff() {
        String propertyValue = property.get();
        // do stuff with propertyValue 

I’m not 100% sure if this is good advice, because this would violate the java beans spec (since the getter and setter are different types) which might confuse groovy

1 Like