tl; dr使用ivyConfigurations
添加自定義配置和resourceGenerators
來處理每個環境的文件。
對於How to Add Environment Profile Config to SBT的回答,所有積分均爲Eugene Yokota。有一些修改,使我的解決方案... 咳嗽 ... 咳嗽 ...稍好。
以下build.sbt
定義了兩種新的配置 - dev的和QA。它還定義resourceGenerators
每個配置,有效地提供了一種方式來訪問什麼樣的配置,新resourceGenerator在執行:
val Dev = config("dev") extend Runtime
val Qa = config("qa") extend Runtime
ivyConfigurations ++= Seq(Dev, Qa)
// http://www.scala-sbt.org/0.13.5/docs/Howto/generatefiles.html#resources
lazy val bareResourceGenerators: Seq[Setting[_]] = Seq(
resourceGenerators += Def.task {
val file = resourceManaged.value/"demo"/"myapp.properties"
println(s"Inside ${configuration.value}")
val contents = s"config=${configuration.value}"
IO.write(file, contents)
Seq(file)
}.taskValue
)
inConfig(Dev)(Defaults.configSettings ++ bareResourceGenerators)
inConfig(Qa)(Defaults.configSettings ++ bareResourceGenerators)
裏面任何你想要的新resourceGenerator你能做到和每個配置的處理是可能的configuration
設置,讓你配置的名稱:
> show dev:configuration
[info] dev
> show qa:configuration
[info] qa
現在當你執行show qa:resources
,你會看到有與target/scala-2.10/resource_managed/qa/demo/myapp.properties
與特定於配置中的內容生成兩個文件:
> show qa:resources
Inside qa
[info] List(/Users/jacek/sandbox/envs/target/scala-2.10/resource_managed/qa/demo/myapp.properties, /Users/jacek/sandbox/envs/src/qa/resources)
的訣竅,現在是使用resourceGenerator,以滿足您的需求,因爲你在Scala代碼是你可以做任何你想要的 - 只使用configuration.value
作爲限定符特定的配置代碼。
說的是,您要在標準src/main/resources
目錄中使用qa
特定屬性文件。只要知道價值在哪裏(什麼配置和價值來自哪裏)。這只是compile:resourceDirectory
。
> show compile:resourceDirectory
[info] /Users/jacek/sandbox/envs/src/main/resources
每當你需要一個 「穩定」(又名配置固定)像src/main/resources
值只要使用resourceDirectory in Compile
。
val props = (resourceDirectory in Compile).value/s"${configuration.value.name}.properties"
println(s"Read files from $props")
通過上述行,你會得到:
> show qa:resources
Inside qa
Read files from /Users/jacek/sandbox/envs/src/main/resources/qa.properties
[info] List(/Users/jacek/sandbox/envs/target/scala-2.10/resource_managed/qa/demo/myapp.properties, /Users/jacek/sandbox/envs/src/qa/resources)
> show dev:resources
Inside dev
Read files from /Users/jacek/sandbox/envs/src/main/resources/dev.properties
[info] List(/Users/jacek/sandbox/envs/target/scala-2.10/resource_managed/dev/demo/myapp.properties, /Users/jacek/sandbox/envs/src/dev/resources)
你究竟是「沒有任何成功」是什麼意思? – reto 2014-10-06 14:10:29
我沒有看到任何輸出,除了已經在我的資源目錄中(沒有錯誤或類似的東西)。正如我所提到的,我希望有一些更好的解決方案,如果我有5個目標,不會強制我添加5個目錄。 – Daniel 2014-10-06 14:36:41