2014-01-10 48 views
9

publish時,如果版本中有-SNAPSHOT,我想用唯一的時間戳替換它。我已經完成了這個工作,除了與+交叉構建外,它已經可以工作了。在SBT中交叉構建時設置唯一的快照版本

似乎我設置的時間戳記的設置version會在跨版本更改時更改爲Scala版本。

當我嘗試跨打造publish我使用下面的命令:

sbt ";stamp-version ;+publish" 

下面是命令stamp-version代碼:

object TimestampVersion { 

    import Version.Snapshot 

    lazy val versionSettings = Seq(commands += stampVersion) 

    def stampVersion = Command.command("stamp-version") { state => 
    val extracted = Project.extract(state) 
    extracted.append(List(version in ThisBuild ~= { ver => 
     val stmp = stamp(ver) 
     Logging.info("Stamping version %s".format(stmp)) 
     stmp 
    }), state) 
    } 

    def stamp(version: String): String = { 
    if (version endsWith Snapshot) { 
     // we use a dot here to not break rpm versioning rules 
     (version stripSuffix Snapshot) + "." + timestamp(System.currentTimeMillis) 
    } else { 
     version 
    } 
    } 

    def timestamp(time: Long): String = { 
    // no delimiter between date & time in order to not break rpm versioning rules 
    val sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss") 
    sdf.setTimeZone(TimeZone.getTimeZone("UTC")) 
    sdf.format(new Date(time)) 
    } 
} 

是否有不同的方法,我可以存儲我可以通過交叉構建訪問的時間戳版本?

編輯:要清楚,我希望每個交叉構建版具有相同的時間戳,這樣,當我使用它從別的地方我可以依靠這樣的:"x" %% "y" % "z"

而且,這將是最好的,如果該解決方案可以與SBT 0.12.x一起工作,因爲我仍然有這種依賴關係。

回答

5

我不確定這是SBT 0.13.x中的推薦方法,但下面的方法似乎對我來說工作得很好。

如果需要「具有相同的時間戳」crossScalaVersions列出的所有版本,那麼你可能想利用Generating files,即IO.writeIO.read方法,保存時間戳文件具有獨特的快照版本。

注意不要設置在build.sbtscalaVersion,因爲它會覆蓋一個由+設置。這是我最初的答案中的問題。

有以下任務build.sbtstampVersion(我把它遷移到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") 

定義命令別名setStampAsVersionAndShowbuild.sbt緩解測試 - 你會看到的versionscalaVersion設置的值在執行命令後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設置和一個其他命令別名setStampAsVersionAndPublishbuild.sbt

publishTo := Some(Resolver.file("file", target.value/"xxx")) 

addCommandAlias("setStampAsVersionAndPublish", 
       "; setVersionFromStampFile ; show scalaVersion ; publish") 

您現在應該能夠publish如你所期望:

  1. 讓我們重新開始。

    [sbt-0-13-1]> clean 
    [success] Total time: 0 s, completed Jan 25, 2014 12:50:22 AM 
    
  2. 生成版本文件。

    [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 
    
  3. 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 
    
  4. 得到最終的命令一展身手 - +進入現場。關於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/) 
    
  5. 顯示基本信息。

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

感謝亞採。我更新了我應該更具體的問題。這個解決方案看起來好像每個版本的Scala都有不同的時間戳組件,它們是交叉構建的,這對我來說並不是那麼有用,因爲它很難依賴於它。 – myyk

+0

@myyk答案已經改變,以反映要求。在回答你的問題時(和掌握SBT)一樣有趣。 –

+0

感謝迄今爲止的一切,這是非常有用的,當我完成後我會分享我的0.12自適應。雖然你的解決方案有問題,但我也一樣,它也在上面的輸出中。設置到版本2.9.3後,你可以看到它發佈到sbt-0-13-1_2.10這是錯誤的,它應該是sbt-0-13-1_2.9.3。 – myyk

相關問題