Inheritance Groovy配置文件中的继承

Inheritance Groovy配置文件中的继承,inheritance,groovy,include,config,Inheritance,Groovy,Include,Config,我需要通过ConfigSlurper定义和读取Groovy配置文件中的几个属性,这些属性将共享一些公共字段,并只添加一个特定字段。大概是这样的: config { // this is something like abstract property common { field1 = 'value1' field2 = 'value2' } property1 { // include fields from common here custo

我需要通过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'
  }
}
我很好奇是否有可能以一种好的方式实现这一点。由于我对Groovy不太熟悉,所以我目前的解决方案并不理想,我想说:

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

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

  property2 = common.clone()
  property2 {
    customField = 'value'
  }
}
config.remove('common')
感谢您的建议,您可以:

config {
    // A common map of values
    def common = [
        field1: 'value1',
        field2: 'value2'
    ] as ConfigObject

    property1 {
        customField = 'value'
    }

    property2 {
        customField = 'value'
    }

    property1.merge(common)
    property2.merge(common)
}

这就是你的意思吗?

是的。这正是我的意思。谢谢你的及时答复。