Deserializing custom plugin configuration

plugins

(Kyle Chamberlin) #1

I have a custom plugin for gradle implemented in Java. we will call it MyPlugin

i include my plugin in the gradle script currently like so:

plugins {
     id 'MyPlugin' version '1.1'
}

ideally, as my plugin grows i would like it to be configurable via closures in the buildscript like this:

pluginConfig {
    option1 = true
    thing {
        first = 10
        second = "foo"
        third {
            a = "bar"
        }
    }
}

i feel like there should be some kind of mechanism (similar to JSON deserializers) that would allow me to define a model in java like this:

class PluginConfig {
    boolean option1;
    Thing thing;

    // getters and setters...
}

class Thing {
    Integer first;
    String second;
    FooBar third;

    // getters and setters...
}

class FooBar {
    String a;

    //getters and setters...
}

Does any facility like this exist? or do i need to write that deserialization logic myself?

any help or leads would be appreciated.


(Stefan Oehme) #2

Gradle uses Groovy, so there is no serialization/deserialization. You are writing build scripts directly against your plugin’s API. Have a look at the user guide for an example on configuration objects. Especially look at Project.extensions and Project.container