在SBT中交叉构建时设置唯一的快照版本

myy*_*yyk 9 scala sbt

如果publish,如果有-SNAPSHOT一个版本,我想了一个独特的时间戳来代替它.我已经完成了这个并且它有效,除了用于交叉构建+.

似乎version每当为交叉构建更改Scala版本时,我设置的时间戳设置都会被清除.

当我尝试交叉构建时,publish我使用以下命令:

sbt ";stamp-version ;+publish"
Run Code Online (Sandbox Code Playgroud)

以下是该命令的代码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))
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过不同的方式存储可以通过交叉构建访问的带时间戳的版本吗?

编辑:要清楚,我希望每个交叉构建版本具有相同的时间戳,以便当我从其他地方使用它时,我可以依赖于这样: "x" %% "y" % "z"

此外,如果解决方案可以与SBT 0.12.x一起使用,那将是最好的,因为我仍然具有该依赖性.

Jac*_*ski 5

我不确定它是否是SBT推荐的方法0.13.x,但以下似乎对我来说效果很好.

如果您需要为列出的所有版本"具有相同的时间戳"crossScalaVersions,那么您可能希望利用生成文件(即IO.writeIO.read方法)来保存具有唯一快照版本的时间戳文件.

注意不要设置scalaVersion,build.sbt因为它会覆盖设置的那个+.这是我最初的答案中的问题.

有以下任务stampVersionbuild.sbt(我把它迁移到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
}
Run Code Online (Sandbox Code Playgroud)

运行任务时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
}
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

为了将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
Run Code Online (Sandbox Code Playgroud)

使用该命令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
Run Code Online (Sandbox Code Playgroud)

在构建定义中build.sbt,定义设置crossScalaVersions,例如

crossScalaVersions := Seq("2.9.3", "2.10.3")
Run Code Online (Sandbox Code Playgroud)

定义命令别名setStampAsVersionAndShowbuild.sbt缓解测试-你会看到的价值versionscalaVersion执行命令后设置setVersionFromStampFile:

addCommandAlias("setStampAsVersionAndShow",
                "; setVersionFromStampFile ; show version; show scalaVersion")
Run Code Online (Sandbox Code Playgroud)

这使您可以交叉执行一系列命令:

[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/)
Run Code Online (Sandbox Code Playgroud)

定义publishTo设置和另一个命令别名setStampAsVersionAndPublishbuild.sbt:

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

addCommandAlias("setStampAsVersionAndPublish",
                "; setVersionFromStampFile ; show scalaVersion ; publish")
Run Code Online (Sandbox Code Playgroud)

你现在应该能够publish如你所愿:

  1. 让我们重新开始吧.

    [sbt-0-13-1]> clean
    [success] Total time: 0 s, completed Jan 25, 2014 12:50:22 AM
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)
  4. 给最后的命令一个 - +进入场景.

    [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/)
    
    Run Code Online (Sandbox Code Playgroud)
  5. 显示有关sbt和构建的基本信息about.

    [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
    
    Run Code Online (Sandbox Code Playgroud)