我不確定這是SBT 0.13.x
中的推薦方法,但下面的方法似乎對我來說工作得很好。
如果需要「具有相同的時間戳」在crossScalaVersions
列出的所有版本,那麼你可能想利用Generating files,即IO.write和IO.read方法,保存時間戳文件具有獨特的快照版本。
注意不要設置在build.sbt
scalaVersion
,因爲它會覆蓋一個由+
設置。這是我最初的答案中的問題。
有以下任務build.sbt
stampVersion
(我把它遷移到SBT < 0.13作爲練習):
lazy val stampVersion = taskKey[File]("Generates timestamp file with unique snapshot version")
stampVersion := {
val log = streams.value.log
val stmp = System.currentTimeMillis
val file = target.value/"timestamp"
log.info(s"Stamping version $stmp saved in $file")
IO.write(file, s"""$stmp""")
file
}
當您運行任務stampVersion
,文件獲取target/timestamp
文件中創建。
通過以下任務,您可以閱讀其內容。
lazy val getStampVersion = taskKey[String]("Retrieves unique snapshot version from timestamp file")
getStampVersion := {
val log = streams.value.log
val file = (target in Compile).value/"timestamp"
val v = IO.read(file)
log.info(s"Retrieving version from $file: $v [scalaVersion: ${scalaVersion.value}]")
v
}
使用show getStampVersion
顯示保存在文件中的版本。
[sbt-0-13-1]> stampVersion
[info] Stamping version 1390606523705 saved in /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp
[success] Total time: 0 s, completed Jan 25, 2014 12:35:23 AM
[sbt-0-13-1]> show getStampVersion
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390606523705 [scalaVersion: 2.10.3]
[info] 1390606523705
[success] Total time: 0 s, completed Jan 25, 2014 12:35:34 AM
爲了version
鍵設定爲時間戳的版本,我定義的命令setVersionFromStampFile
(因爲它改變一個項目的狀態)。
def setVersionFromStampFile = Command.command("setVersionFromStampFile") { state =>
val e = Project extract state
import e._
val (newState, stampVersion) = runTask(getStampVersion, state)
val scalaV = scalaVersion in currentRef get structure.data getOrElse Nil
state.log.info(s"scalaVersion: $scalaV")
val settings = Seq(
version := stampVersion
)
append(settings ++ structure.settings, state)
}
commands += setVersionFromStampFile
隨着命令setVersionFromStampFile
,一旦進入運行,version
得到適當設置。
[sbt-0-13-1]> show version
[info] 0.1-SNAPSHOT
[sbt-0-13-1]> setVersionFromStampFile
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390606523705 [scalaVersion: 2.10.3]
[info] scalaVersion: 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[sbt-0-13-1]> show version
[info] 1390606523705
在構建定義build.sbt
,已經設定crossScalaVersions
限定,例如
crossScalaVersions := Seq("2.9.3", "2.10.3")
定義命令別名setStampAsVersionAndShow
在build.sbt
緩解測試 - 你會看到的version
和scalaVersion
設置的值在執行命令後setVersionFromStampFile
:
addCommandAlias("setStampAsVersionAndShow",
"; setVersionFromStampFile ; show version; show scalaVersion")
這使您可以跨執行一系列命令:
[sbt-0-13-1]> + setStampAsVersionAndShow
[info] Setting version to 2.9.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390606523705 [scalaVersion: 2.9.3]
[info] scalaVersion: 2.9.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] 1390606523705
[info] 2.9.3
[info] Setting version to 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390606523705 [scalaVersion: 2.10.3]
[info] scalaVersion: 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] 1390606523705
[info] 2.10.3
[info] Setting version to 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
定義publishTo
設置和一個其他命令別名setStampAsVersionAndPublish
在build.sbt
:
publishTo := Some(Resolver.file("file", target.value/"xxx"))
addCommandAlias("setStampAsVersionAndPublish",
"; setVersionFromStampFile ; show scalaVersion ; publish")
您現在應該能夠publish
如你所期望:
讓我們重新開始。
[sbt-0-13-1]> clean
[success] Total time: 0 s, completed Jan 25, 2014 12:50:22 AM
生成版本文件。
[sbt-0-13-1]> stampVersion
[info] Stamping version 1390607428495 saved in /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp
[success] Total time: 0 s, completed Jan 25, 2014 12:50:28 AM
與publish
檢查它無交叉建築物(無+
)。
[sbt-0-13-1]> setStampAsVersionAndPublish
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390607428495 [scalaVersion: 2.10.3]
[info] scalaVersion: 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] 2.10.3
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495-sources.jar ...
[info] Done packaging.
[info] Updating {file:/Users/jacek/sandbox/so/sbt-0.13.1/}sbt-0-13-1...
[info] Wrote /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] :: delivering :: default#sbt-0-13-1_2.10;1390607428495 :: 1390607428495 :: release :: Sat Jan 25 00:50:33 CET 2014
[info] delivering ivy file to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/ivy-1390607428495.xml
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/classes...
[info] Main Scala API documentation to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/api...
model contains 2 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495.jar ...
[info] Done packaging.
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495.pom
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495.jar
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495-sources.jar
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495-javadoc.jar
[success] Total time: 1 s, completed Jan 25, 2014 12:50:34 AM
得到最終的命令一展身手 - +
進入現場。關於SBT與about
構建
[sbt-0-13-1]> + setStampAsVersionAndPublish
[info] Setting version to 2.9.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390607428495 [scalaVersion: 2.9.3]
[info] scalaVersion: 2.9.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] 2.9.3
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/sbt-0-13-1_2.9.3-1390607428495-sources.jar ...
[info] Done packaging.
[info] Updating {file:/Users/jacek/sandbox/so/sbt-0.13.1/}sbt-0-13-1...
[info] Wrote /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/sbt-0-13-1_2.9.3-1390607428495.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] :: delivering :: default#sbt-0-13-1_2.9.3;1390607428495 :: 1390607428495 :: release :: Sat Jan 25 00:50:51 CET 2014
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/classes...
[info] delivering ivy file to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/ivy-1390607428495.xml
[info] Main Scala API documentation to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/api...
model contains 2 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/sbt-0-13-1_2.9.3-1390607428495-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.9.3/sbt-0-13-1_2.9.3-1390607428495.jar ...
[info] Done packaging.
[info] published sbt-0-13-1_2.9.3 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.9.3/1390607428495/sbt-0-13-1_2.9.3-1390607428495.pom
[info] published sbt-0-13-1_2.9.3 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.9.3/1390607428495/sbt-0-13-1_2.9.3-1390607428495.jar
[info] published sbt-0-13-1_2.9.3 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.9.3/1390607428495/sbt-0-13-1_2.9.3-1390607428495-sources.jar
[info] published sbt-0-13-1_2.9.3 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.9.3/1390607428495/sbt-0-13-1_2.9.3-1390607428495-javadoc.jar
[success] Total time: 4 s, completed Jan 25, 2014 12:50:54 AM
[info] Setting version to 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] Retrieving version from /Users/jacek/sandbox/so/sbt-0.13.1/target/timestamp: 1390607428495 [scalaVersion: 2.10.3]
[info] scalaVersion: 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[info] 2.10.3
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495-sources.jar ...
[info] Done packaging.
[info] Updating {file:/Users/jacek/sandbox/so/sbt-0.13.1/}sbt-0-13-1...
[info] Wrote /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] :: delivering :: default#sbt-0-13-1_2.10;1390607428495 :: 1390607428495 :: release :: Sat Jan 25 00:50:55 CET 2014
[info] delivering ivy file to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/ivy-1390607428495.xml
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/classes...
[info] Main Scala API documentation to /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/api...
model contains 2 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/jacek/sandbox/so/sbt-0.13.1/target/scala-2.10/sbt-0-13-1_2.10-1390607428495.jar ...
[info] Done packaging.
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495.pom
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495.jar
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495-sources.jar
[info] published sbt-0-13-1_2.10 to /Users/jacek/sandbox/so/sbt-0.13.1/target/xxx/default/sbt-0-13-1_2.10/1390607428495/sbt-0-13-1_2.10-1390607428495-javadoc.jar
[success] Total time: 2 s, completed Jan 25, 2014 12:50:56 AM
[info] Setting version to 2.10.3
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
顯示基本信息。
[sbt-0-13-1]> about
[info] This is sbt 0.13.1
[info] The current project is {file:/Users/jacek/sandbox/so/sbt-0.13.1/}sbt-0-13-1 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3
感謝亞採。我更新了我應該更具體的問題。這個解決方案看起來好像每個版本的Scala都有不同的時間戳組件,它們是交叉構建的,這對我來說並不是那麼有用,因爲它很難依賴於它。 – myyk
@myyk答案已經改變,以反映要求。在回答你的問題時(和掌握SBT)一樣有趣。 –
感謝迄今爲止的一切,這是非常有用的,當我完成後我會分享我的0.12自適應。雖然你的解決方案有問題,但我也一樣,它也在上面的輸出中。設置到版本2.9.3後,你可以看到它發佈到sbt-0-13-1_2.10這是錯誤的,它應該是sbt-0-13-1_2.9.3。 – myyk