...
| Code Block |
|---|
def config = new ConfigSlurper().parse(..)
new File("..").withWriter { writer ->
config.writeTo(writer)
}
|
特殊な "環境定義" 構成
| Excerpt |
|---|
|
Special "environments" Configuration |
ConfigSlurperクラスには「環境」パラメータ引数に持つ、デフォルトコンストラクタと異なるコンストラクタが存在します。
この特別なコンストラクタは環境定義ファイルと連携して動きます。
既存の環境定義設定は環境定義クロージャの定義によって上書きすることができます。
複数の関連した構成が同じファイルに保存することも可能です。
| Excerpt |
|---|
|
The ConfigSlurper class has a special constructor other than the default constructor that takes an "environment" parameter. This special constructor works in concert with a property setting called environments. This allows a default setting to exist in the property file that can be superceded by a setting in the appropriate environments closure. This allows multiple related configurations to be stored in the same file. |
構成の設定ファイル例
| Excerpt |
|---|
|
Given this groovy property file: |
| Code Block |
|---|
Sample.groovy
sample {
foo = "default_foo"
bar = "default_bar"
}
environments {
development {
sample {
foo = "dev_foo"
}
}
test {
sample {
bar = "test_bar"
}
}
}
|
この構成を実行するコードは下記となります:
| Excerpt |
|---|
|
Here is the demo code that exercises this configuration |
| Code Block |
|---|
def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL())
assert config.sample.foo == "dev_foo"
assert config.sample.bar == "default_bar"
config = new ConfigSlurper("test").parse(new File('Sample.groovy').toURL())
assert config.sample.foo == "default_foo"
assert config.sample.bar == "test_bar"
|
追記:
環境定義のクロージャは直接解析されるわけでありません。
特別な環境下で使われる場合を除いて、このコンストラクタのクロージャは無視されます
| Excerpt |
|---|
|
Note: the environments closure is not directly parsable. Without using the special environment constructor the closure is ignored. |
環境定義値のコンストラクタには構成ファイルを利用できます。
下記のような構成を設定する事もできます:
| Excerpt |
|---|
|
The value of the environment constructor is also available in the configuration file, allowing you to build the configuration like this: |
...