Groovy:字段属性被多次声明

时间:2016-04-28 07:29:15

标签: gradle groovy

我想从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:字段属性多次声明

现在,如果我在apply方法中编写它,那么它可以工作,但我需要在apply方法之外使用componentClass变量,所以我需要在外面写这个。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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')
  }
}