2016-04-22 74 views
3

我需要定义和读取ConfigSlurper Groovy配置文件中的几个属性,它将共享一些常用字段并只添加一个特定字段。这样的事情: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') 

感谢您的任何意见

回答

0

你可以这样做:

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) 
} 

是那个你是什​​么意思?

+0

是的。这正是我的意思。感谢您的及时答复。 –