2016-04-28 82 views
0

我想从gradle.properties获得我的自定义插件类中的变量值。但我想在apply方法之外编写和使用它。所以,我写这样的:Groovy:字段属性被声明多次

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

现在,如果我写的申请方法里面,那么它的工作原理,但我需要在室外使用componentClass变量申请方法,所以我需要写在外面。任何帮助将不胜感激。

回答

1

下面的代码应该做的工作:

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') 
    } 
} 
+0

是的,这worked.I在想不同的东西。这个简单的解决方案并没有让我想起。任何方式感谢您的帮助。 – sver