Dotted properties in Gradle

Hello,

I’m new to Gradle and evaluating it to replace Ant builds.

I’m wondering how does Gradle work with dotted properties?

Can I set “project.build.customProperty” ? In my Ant builds I use the dots to offer a compact syntax for accessing properties related to certain tasks in an organized manner. I know Gradle uses its own structure (project { build { customProperty = … } } }.) for nested properties.

Can I still use dotted property syntax in Gradle? And can it convert over to Gradle’s nested properties?

Thanks!

You can use dotted properties, but I don’t recommend it. You will have to use special syntax for accessing them in the build script, and they don’t translate to nested properties. Dotted properties are less needed in Gradle than Ant because:

  • A typical Gradle build has far fewer user-defined properties because Gradle has an extensive build (object) model

  • User-defined properties can not just be defined on ‘project’ but also on nested objects. (Search for extra properties in the Gradle User Guide.)

Thanks for the reply. What would the dotted syntax look like in the Gradle scripts?

I understand the concept of build objects having properties, however I would like to keep a Java-like properties file for setting build configuration versus a Groovy syntax.

Are there any tips for implementing such a scheme as a plugin? Basically parsing a properties file and converting them to nested properties and vice versa?

What would the dotted syntax look like in the Gradle scripts?

To set a project property, you would use ‘setProperty(“foo.bar”, someValue)’. To read it, you would use ‘property(“foo.bar”)’.

I understand the concept of build objects having properties, however I would like to keep a Java-like properties file for setting build configuration versus a Groovy syntax.

The drawback of this approach is that you will end up using two build languages: a (very limited) properties language, and the Gradle build script language. I’ve seen many people starting out with this approach (because it seemed more familiar) and eventually switching over to a pure Gradle build script approach.

Are there any tips for implementing such a scheme as a plugin? Basically parsing a properties file and converting them to nested properties and vice versa?

You’d use Groovy meta-programming. For example, you might loop through the parts of the dotted property expression and get/set them with ‘currentObject."$part"’.

I have a tiny plugin that automatically converts dotted properties into nested maps. https://github.com/esycat/gradle-props-util

you can use ConfigSlurper to get access to dotted properties like this:

def props = new Properties()
file("foo.properties").withInputStream { props.load(it) }
 ext.params = new ConfigSlurper().parse(props)

Assuming inside ‘foo.properties’ you have defined a dotted property “x.y.z”, you would be able to access it in the build as “params.x.y.z”.

You may also provide aliases like this:

ext.subParams = params.deeply.nested.subtree
 ext.subParamsOptional = params.another?.deeply?.nested?.subtree

Now, instead of specifying ‘params.deeply.nested.subtree.foo.bar.baz’, you can use ‘subParams.foo.bar.baz’

Finally, you can add structure and reorganize by using combination of maps and config objects:

ext.mapped = [
     foo: params.deeply.nested.subtree,
     bar: params.differently.nested.subtree,
     baz: [ synthetic: "value" ]
 ]

While you can do all these, I would agree with Peter, that they don’t add much value and may sidetrack you in a direction where you are fighting the tool rather than using it.

Note that I could only get this to work as follows:

project.property(‘my.property’)

Omitting the ‘project’ bit caused the property not to be found. I set my properties in gradle.properties.