2014-10-06 17 views
3

我目前正在從Maven遷移到SBT,並且我正在努力瞭解如何處理多個構建目標(開發,測試,培訓,產品等)。如何處理多個構建目標,例如開發,測試,主要?

例如,我有一個persistence.xml,看起來像這樣:

<properties> 
    <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/> 
    <property name="javax.persistence.jdbc.url" value="${db.connectionURL}"/> 
    <property name="javax.persistence.jdbc.user" value="${db.username}"/> 
    <property name="javax.persistence.jdbc.password" value="${db.password}"/> 
    <property name="eclipselink.target-database" value="Oracle10"/> 
</properties> 

與Maven,這是很容易處理這種使用配置文件。

我已經嘗試過這裏爲SBT提出的建議,但我沒有采用這種方法取得任何成功。 How to Add Environment Profile Config to SBT

另外,使用這種方法我需要爲每個新環境提供一個新的目錄。我只是覺得必須有更好的方法來處理這種使用SBT的設置?

+0

你究竟是「沒有任何成功」是什麼意思? – reto 2014-10-06 14:10:29

+0

我沒有看到任何輸出,除了已經在我的資源目錄中(沒有錯誤或類似的東西)。正如我所提到的,我希望有一些更好的解決方案,如果我有5個目標,不會強制我添加5個目錄。 – Daniel 2014-10-06 14:36:41

回答

2

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) 
+0

嗨,你現在有沒有一個有據可查的文檔教程關於這個? – MaatDeamon 2015-07-09 18:29:40

+0

你需要多少錢?你目前想念什麼? – 2015-07-09 22:42:55

+0

謝謝。 Intellij Idea支持這種方法嗎?例如。有沒有能力選擇配置? – 2016-05-06 08:24:28

相關問題