小编Sta*_*vec的帖子

Groovy配置文件中的继承

我需要通过ConfigSlurper定义并读取Groovy配置文件中的几个属性,该文件将共享一些公共字段并仅添加一个特定字段。像这样:

config {
  // this is something like abstract property
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 {
    // include fields from common here
    customField = 'prop1value'
  }

  property2 {
    // include fields from common here
    customField = 'prop2value'
  }
}
Run Code Online (Sandbox Code Playgroud)

我很想知道是否有可能以一种不错的方式实现这一目标。由于我对Groovy不太熟悉,所以我当前的解决方案并不理想,我会说:

config {
  common {
    field1 = 'value1'
    field2 = 'value2'
  }

  property1 = common.clone()
  property1 {
    customField = 'value'
  }

  property2 = common.clone()
  property2 {
    customField = 'value'
  }
}
config.remove('common')
Run Code Online (Sandbox Code Playgroud)

感谢您的任何建议

groovy inheritance config include

5
推荐指数
1
解决办法
166
查看次数

标签 统计

config ×1

groovy ×1

include ×1

inheritance ×1