Adding custom Data Type in Gradle


(Sourabh) #1

I have custom DataTypes in Ant which ant loads automatically into Java Bean Pojo’s. I am trying to understand if there is similar DataType object in Gradle which can directly gets loaded from a XML configuration file? This is a very common usecase , but not sure why Gradle dont have such DataType support.


(Dimitar Dimitrov) #2

Gradle has plugin extensions which define blocks in the build where you can specify custom data. As the Gradle build is Groovy or Kotlin, typically the data iwhas specified directly using Groovy and Kotlin DSL. That said, nothing is stopping you from writing a 5 line method loading an XML file or whatever fancy serialized format you prefer.

An example of what you can do with extensions is the “distributions” block defined by the DistributionPluginExtension (pretty sure that’s the name as conventions are fairly consistent in the Gradle codebase). Read the plugins task guide for details.

One last note, if you want to manage a collection of data objects, make sure you use a DomainObjectCollection or NamedDomainObjectCollection. Read the documentation for these classes and learn how to take advantage of them to keep your custom DSL consistent with the rest of Gradle.


(Sourabh) #3

Thanks much Dimitar. To answer to your question -

Yes I can write any JAXB to map xml to POJO , but i was looking for some kind of default frame work where data from XML gets loaded directly in POJO object , the way ant does it.

I looked into NamedDomainObjectContainer , but I guess it accepts unique values . My sameple xmls structure will be something like below . How can I map below?

shapes.xml (236 Bytes)


(Sourabh) #4

(Dimitar Dimitrov) #5

Is there a reason to insist on describing your domain objects as xml? Both Groovy and Kotlin provide nicer syntax for defining data (see Groovy’s @Canonical and Kotlin’s data classes), and the domain object collections give you a very pleasant DSL for working with them.

If you have to do xml for compatibility reasons, I suggest you look at the XmlSlurper - JAXB only adds ceremony and doesn’t give you much in the context of writing build scripts.