Call constructor on extension


(Matthew Jones) #1

I am writing a custom plugin for Gradle. I want to be able to have:

serviceDependencies {
    service name: 'service1', version: '1.0'
    service name: 'service2', version: '1.1'

In my Plugin implementation (in Java) I have:

public void apply(final Project project) {
    project.getExtensions().create("serviceDependencies", Services.class);
    project.getExtensions().create("service", Service.class);


public class Service {
    private String name;
    private String version;

    public Service(final String name, final String version) { = name;
        this.version = version;

    public String getName() {

    public void setName(final String name) { = name;

    public String getVersion() {
        return this.version;

    public void setVersion(final String version) {
        this.version = version;

When I try use this plugin I get:
java.lang.IllegalArgumentException: Could not find any public constructor for class which accepts parameters [].

This still happens when I remove serviceDependencies/ from the picture.

If I remove the Service constructor or remove the arguments:
org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method service() for arguments [{name=service1, version=1.0}] on root project ...

Obviously my pojo is being decorated, but not quite with the correct constructor. How can I get the constructor to work how I want in my build.gradle script?

A second and independent question is what should look like?

(Stefan Oehme) #2

Have a look at the user guide for an example of maintaining multipe domain objects.