Understanding Lazy Configuration

I try to understand lazy properties as described here: Lazy Configuration

I’d like to be able to change the value of a custom task property during execution phase. As far as I understand the documentation, this should be possible:

Lazy properties are intended to be passed around and only queried when required. Usually, this will happen during the execution phase.

The property in my custom task is defined like this:

@Input
public abstract Property<String> getModeldir();

The build file:

def foo = "a"

task fubar() {
    doLast {
        foo = "b"
        println foo
    }
}

task validateData(type: MyCustomTask, dependsOn: 'fubar') {
    modeldir = foo
}

In my custom task getModeldir().get() will still output “a” and not “b” as expected. So I guess my expectation is wrong.

Thanks for any clarification.

Stefan

This is true, but that’s not what you’re doing.

You are (in the order the code executes):

  • defining a variable (which is not a lazy property) and setting its value (def foo = "a").
  • setting the lazy property to the current value of the variable (modeldir = foo).
  • resetting the value of the defined variable, which has no impact on the lazy property (foo = "b").

Thanks for the answer. The solution I came up with is:

def foo = objects.property(String)

Stefan