Gradle 基于规则的模型嵌套 beans 语法
Gradle rule-based model nested beans syntax
我想尝试 new rule based configuration in Gradle 配置我的插件。插件的目的是将数据上传到云存储,如 Google Drive 或 S3。我需要为用户想要使用的每个存储进行配置。所以,我想要实现的是:
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)
}
}
}
在我的插件中,我为 backup
元素配置规则源:
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)
}
但是,这样说是行不通的:Could not find method googleDrive() for arguments [build_8w85xu7hrz3atgeg839d33hzl$_run_closure1_closure2_closure3@1b06ac95] on root project 'test'.
。看起来它试图在 backup
中调用方法而不是配置嵌套 bean。
那么,正确的语法是什么?
我知道命名集合,但问题是如何配置自定义 bean 层次结构。
这是一个 bean 层次结构集合的模型示例,灵感来自 Gradle 包 samples/modelRules/ruleSourcePluginEach/
。
BackupStorage
包含 GoogleDrive
和 S3
存储的共同属性。
apply plugin: BackupPlugin
model {
backup {
storage1(GoogleDrive) {
clientId = '1'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
storage2(S3) {
clientId = '2'
}
storage3(GoogleDrive) {
clientId = '3'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
}
}
class BackupPlugin implements Plugin<Project> {
void apply(Project p) { }
static class Rules extends RuleSource {
@Model void backup(ModelMap<BackupStorage> bs) {
}
}
}
@Managed
interface BackupStorage {
String getClientId()
void setClientId(String clientId)
}
@Managed
interface GoogleDrive extends BackupStorage {
String getClientSecret()
void setClientSecret(String clientSecret)
String getAccessToken()
void setAccessToken(String accessToken)
String getRefreshToken()
void setRefreshToken(String refreshToken)
}
@Managed
interface S3 extends BackupStorage {
}
玩了一会儿后,似乎让 googleDrive
只读(即删除 setGoogleDrive()
setter)解决了你的问题(当 运行 gradle model
时我得到 BUILD SUCCESSFUL)。
经过一些小的修改(使用 @Mutate
而不是 Plugin.apply
),它看起来像这样:
apply plugin: BackupPlugin
class BackupPlugin extends RuleSource {
@Model
void backup(BackupPluginExtension backupModel) {
}
@Mutate void createTasks(ModelMap<Task> tasks, BackupPluginExtension model) {
tasks.create("backup") {
doLast {
// ... do stuff
println "googleDrive.clientId = ${model.googleDrive.clientId}"
}
}
}
}
@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)
}
model {
backup { // this is a container for all storages I want to support
googleDrive {
clientId = 'someId'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
}
}
运行 gradle backup
在此打印 googleDrive.clientId = someId
.
我想尝试 new rule based configuration in Gradle 配置我的插件。插件的目的是将数据上传到云存储,如 Google Drive 或 S3。我需要为用户想要使用的每个存储进行配置。所以,我想要实现的是:
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)
}
}
}
在我的插件中,我为 backup
元素配置规则源:
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)
}
但是,这样说是行不通的:Could not find method googleDrive() for arguments [build_8w85xu7hrz3atgeg839d33hzl$_run_closure1_closure2_closure3@1b06ac95] on root project 'test'.
。看起来它试图在 backup
中调用方法而不是配置嵌套 bean。
那么,正确的语法是什么?
我知道命名集合,但问题是如何配置自定义 bean 层次结构。
这是一个 bean 层次结构集合的模型示例,灵感来自 Gradle 包 samples/modelRules/ruleSourcePluginEach/
。
BackupStorage
包含 GoogleDrive
和 S3
存储的共同属性。
apply plugin: BackupPlugin
model {
backup {
storage1(GoogleDrive) {
clientId = '1'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
storage2(S3) {
clientId = '2'
}
storage3(GoogleDrive) {
clientId = '3'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
}
}
class BackupPlugin implements Plugin<Project> {
void apply(Project p) { }
static class Rules extends RuleSource {
@Model void backup(ModelMap<BackupStorage> bs) {
}
}
}
@Managed
interface BackupStorage {
String getClientId()
void setClientId(String clientId)
}
@Managed
interface GoogleDrive extends BackupStorage {
String getClientSecret()
void setClientSecret(String clientSecret)
String getAccessToken()
void setAccessToken(String accessToken)
String getRefreshToken()
void setRefreshToken(String refreshToken)
}
@Managed
interface S3 extends BackupStorage {
}
玩了一会儿后,似乎让 googleDrive
只读(即删除 setGoogleDrive()
setter)解决了你的问题(当 运行 gradle model
时我得到 BUILD SUCCESSFUL)。
经过一些小的修改(使用 @Mutate
而不是 Plugin.apply
),它看起来像这样:
apply plugin: BackupPlugin
class BackupPlugin extends RuleSource {
@Model
void backup(BackupPluginExtension backupModel) {
}
@Mutate void createTasks(ModelMap<Task> tasks, BackupPluginExtension model) {
tasks.create("backup") {
doLast {
// ... do stuff
println "googleDrive.clientId = ${model.googleDrive.clientId}"
}
}
}
}
@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)
}
model {
backup { // this is a container for all storages I want to support
googleDrive {
clientId = 'someId'
clientSecret = ''
accessToken = ''
refreshToken = ''
}
}
}
运行 gradle backup
在此打印 googleDrive.clientId = someId
.