我想从gradle.properties
获取自定义插件类中变量的值。但是我想在apply
方法之外编写和使用它。所以,我这样写:
class VCPlugin implements Plugin<Project> {
private Project project
private Properties properties
properties = new Properties()
properties.load(project.rootProject.file('gradle.properties').newDataInputStream())
def componentClass = properties.getProperty('componentClass')
@Override
void apply(Project project) {
//applying distribution plugin
this.project = project .....
}
}
但这会产生编译错误:
Groovy:The field properties is declared multiple times
现在,如果我在apply方法中编写它,那么它可以工作,但是我需要在apply方法之外使用
componentClass
变量,因此我需要在外部编写它。任何帮助将不胜感激。
最佳答案
下面的代码可以完成这项工作:
class VCPlugin implements Plugin<Project> {
private Project project
private Properties properties
private String componentClass
@Override
void apply(Project project) {
this.project = project
this.properties = new Properties()
properties.load (project.rootProject.file('gradle.properties').newDataInputStream())
this.componentClass = this.properties.getProperty('componentClass')
}
}
关于gradle - Groovy:多次声明字段属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36908087/