Gradle rule-based model nested beans syntax

software-model
plugins

(Siarhei) #1

I want to try new rule based configuration in Gradle to configure my plugin. Plugin’s purspose is uploading data to cloud storages, like Google Drive or S3. I need a configuration for each storage the user want’s to use. So, what I want to achieve is:

model {
    backup { // this is a container for all storages I want to support
        googleDrive {
            clientId = ''
            clientSecret = ''
            accessToken = ''
            refreshToken = ''
        }

        s3 {
           // S3 specific config (like API keys)
        }
    }
}

In my plugin I configure rule source for backup element:

class BackupPlugin implements Plugin<Project> {

    static class Rules extends RuleSource {
        @Model
        void backup(BackupPluginExtension backupModel) {}
    }
}

@Managed
interface BackupPluginExtension {
    GoogleDrive getGoogleDrive()

    void setGoogleDrive(GoogleDrive googleDrive)
}

@Managed
interface GoogleDrive {
    String getClientId()

    void setClientId(String clientId)

    String getClientSecret()

    void setClientSecret(String clientSecret)

    String getAccessToken()

    void setAccessToken(String accessToken)

    String getRefreshToken()

    void setRefreshToken(String refreshToken)
}

But, that doesn’t work saying: Could not find method googleDrive() for arguments [build_8w85xu7hrz3atgeg839d33hzl$_run_closure1_closure2_closure3@1b06ac95] on root project 'test'.. Looks like it tries to call methods inside backup and not configure nested beans.

So, what is the correct syntax for that?


(Andreas Axelsson) #2

For managed sub-types, if you want Gradle to instantiate the object, you need to not supply a setter. It’s not entirely clear to me how to use this if one has a setter, but in your case I’m pretty sure it’ll work if you just omit it.

@Managed
interface BackupPluginExtension {
    GoogleDrive getGoogleDrive()
}

(Siarhei) #3

Well, the problem was Gradle version. Looks like some bug in 2.9. In latest version everything works with or without setter. The question is already answered here.