2016-04-22 70 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

是的。這正是我的意思。感謝您的及時答覆。 –