Slackbot
10/24/2022, 10:44 AMAlon Eitan
10/24/2022, 10:46 AMgradle greeting -Pgreeting.message="hello3"
then I get the output
hello2 and salutations2
What I understand is happening is that during the configuration phase, the greeting {} extension's properties are not yet accessible, so when extension.getMessage().get() is called, it returns the value set in the convention method
Is there a way to get the value defined in the greeting {} extension (i.e. the value 'hello2' ) during the configuration phase?Anze Sodja
10/24/2022, 11:14 AMget() at configuration time, but you use for example map() . In your case you could do:
Property<String> message = extension.getMessage().map(it -> {
// if else logic here
})
project.getTasks().register("greetingTask", task -> {
// Note: Accessing a property outside of doFirst/doLast or methods annotated with @TaskAction is actually a configuration phase
doLast(task -> {
// Accessing a property in doFirst/doLast is an execution phase
System.out.println(message.get())
});
});Anze Sodja
10/24/2022, 11:18 AM.get() at configuration time you probably have to fallback to use project.afterEvaluate() , but that goes against the idea of Properties and “everything is lazy” principle.Alon Eitan
10/24/2022, 11:19 AMAnze Sodja
10/24/2022, 11:26 AM-Pgreeting.message then you make this a task input via project.getProviders().gradleProperty("greeting.message") . And then inside a task action you do if/else or some logic and decide what will you do with that user input.
But yeah, for some special cases that might be sometimes difficult.Vampire
10/24/2022, 11:47 AMgreeting {
setMessage('hello2')
}
and then you can do the relevant things in the setMessage function of the the extension.Alon Eitan
10/24/2022, 12:32 PM-Pflags and via the ext {} block in the build.gradle file.
These gradle properties are always accessible, so I can use them to decide stuff during the configuration phaseAlon Eitan
10/24/2022, 12:33 PMgreeting{} block above)
The disadvantage is that I can no longer access these properties during configuration phaseAlon Eitan
10/24/2022, 12:33 PM