ExtraPropertiesExtension

I have been reading about the extra properties. Have seen them in a lot of code and some of the plugins I use, use them.

I read the following here “Wherever possible, the Groovy property syntax should be preferred over the ExtraPropertiesExtension.get() and ExtraPropertiesExtension.set() methods.”

What then is the purpose for the ExtraPropertiesExtension and where would I use it?

All this means is that ext.foo = 'bar' is preferred over ext.set('foo', 'bar').

Clearly I didn’t interpret the statement correctly.

Back to what I am trying to achieve.

I wrote a corporate plug-in which is used across all projects in the company.

The first function in my plug-in is to read all the properties from gradle.properties and set default values for those values not configured. For example:

def repoUrl = project.hasProperty(“repoUrl”) ? project.getProperty("repoUrl) : “http://myhost/repo

This repoUrl is used through out the rest of the plug-in and projects using the plug-in.

What is the recommended way to use it? Should I use the extra properties extension (ext.repoUrl = repoUrl) or the normal project properties (project.setProperty(“repoUrl”, repoUrl))

Through out the project I can then use it like this:
maven { url project.getProperty(“repoUrl”) }
or this:
maven { url project.ext.repoUrl }

My question is, what is best practice, to use project properties or extra properties extenion?

The main difference here is that calling setProperty() will fail if the property doesn’t already exist. In general, extra properties are recommended.

1 Like