我正在为Gradle编写一个自定义插件。我希望能够拥有:
autoDeploy=true
在我的插件实现中(在Java中)我有:
serviceDependencies {
service name: 'service1', version: '1.0'
service name: 'service2', version: '1.1'
}
和Service.java:
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) {
this.name = name;
this.version = version;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
public String getVersion() {
return this.version;
}
public void setVersion(final String version) {
this.version = version;
}
}
当我从图片中删除serviceDependencies / Services.java时,仍然会发生这种情况。
如果我删除了Service构造函数或删除了参数。
java.lang.IllegalArgumentException: Could not find any public constructor for class com.xxx.xxx.Service_Decorated which accepts parameters [].
显然我的pojo正在装饰,但不完全正确的构造函数。如何在build.gradle脚本中使构造函数按照我的方式工作?
第二个独立的问题是Services.java应该是什么样的?
答案 0 :(得分:0)
我只会为serviceDependencies {}
注册一个扩展程序,该扩展程序公开用于注册您的服务的功能:
public class Services {
void service(String name, String version) { /* new Service(...) */ }
}
project.getExtensions().create("serviceDependencies", Services.class);
这将允许Java,Kotlin和Groovy消费者执行类似的操作:
serviceDependencies {
service 'service1', '1.0'
service 'service2', '0.1'
}
然后,如果要支持Groovy命名参数,则需要添加:
public class Services {
void service(String name, String version) { /* new Service(...) */ }
void service(Map<String, String> namedArguments) {
service(namedArguments.get("name"), namedArguments.get("version"))
}
}
这将使Groovy消费者可以这样做:
serviceDependencies {
service name: 'service1', version: '1.0'
service name: 'service2', version: '0.1'
}