Last active
January 4, 2016 15:49
-
-
Save myyk/8642799 to your computer and use it in GitHub Desktop.
Setting timestamp on snapshot versions with SBT 0.12.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commons.admin | |
import sbt._ | |
import sbt.Keys._ | |
import com.myCompany | |
import com.myCompany.{rootProject, jarProject} | |
object Build extends sbt.Build { | |
def scalaVersions = Seq("2.9.1", "2.10.2") | |
def crossBuild = true | |
def scalaVersionSetting: Seq[Setting[_]] = { | |
if (crossBuild) { | |
Seq( | |
// When I didn't set scalaVersion it was building with version 2.9.2 which was very wrong. | |
sbt.Keys.scalaVersion in ThisBuild := "2.9.1", | |
sbt.Keys.crossScalaVersions in ThisBuild := scalaVersions | |
) | |
} else { | |
Seq.empty | |
} | |
} | |
lazy val commonsAdmin = rootProject( | |
id = "commons-admin", | |
settings = scalaVersionSetting, | |
aggregate = Seq(core, play) | |
) | |
lazy val core = jarProject( | |
id = "commons-admin-core", | |
base = file("core"), | |
settings = Seq(libraryDependencies ++= Dependencies.core) | |
) | |
lazy val play = jarProject( | |
id = "commons-admin-play", | |
base = file("play"), | |
dependencies = Seq(core), | |
settings = Seq(libraryDependencies <++= Dependencies.play) ++ myCompany.forkSourceSettings | |
) | |
} | |
object Dependencies { | |
val core = Seq( | |
// Doesn't matter | |
) | |
val myPlay = Seq( | |
// Doesn't matter | |
) | |
def play: Project.Initialize[Seq[ModuleID]] = { | |
(scalaVersion) { scalaVer => | |
val play = if (scalaVer startsWith "2.9") { | |
Seq("play" %% "play" % "2.0.4", | |
"play" %% "play-test" % "2.0.4" % "test") | |
} else { | |
Seq("com.typesafe.play" %% "play" % "2.2.1", | |
"com.typesafe.play" %% "play-test" % "2.2.1" % "test") | |
} | |
play ++ myPlay | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myCompany | |
import sbt._ | |
import sbt.Keys._ | |
import java.util.{Date, TimeZone} | |
object TimestampVersion { | |
import Version.Snapshot | |
lazy val versionSettings = Seq(stampVersionTask, getStampVersionTask, commands += setVersionFromStampFile) | |
val timestampFileName = "timestamp" | |
lazy val stampVersion = TaskKey[File]("stamp-version", "Generates timestamp file with unique snapshot version") | |
lazy val stampVersionTask = stampVersion <<= (version in ThisBuild, target) map { (ver, tar) => | |
val stmp = stamp(ver) | |
val file = tar / timestampFileName | |
Logging.info("Stamping version %s".format(stmp)) | |
IO.write(file, stmp) | |
file | |
} | |
lazy val getStampVersion = TaskKey[String]("get-stamp-version", "Retrieves unique snapshot version from timestamp file") | |
lazy val getStampVersionTask = getStampVersion <<= (target in Compile, scalaVersion) map { (tar, scalaV) => | |
val file = tar / timestampFileName | |
val version = IO.read(file) | |
Logging.info("Retrieving version from %s: %s [scalaVersion: %s]".format(file, version, scalaV)) | |
version | |
} | |
def setVersionFromStampFile = Command.command("set-version-from-stamp-file") { state => | |
val extracted = Project extract state | |
import extracted._ | |
val (newState, stampVersion) = runTask(getStampVersion, state) | |
val scalaV = scalaVersion in currentRef get structure.data getOrElse "" | |
Logging.info("Setting version to %s [scalaVersion: %s]".format(stampVersion, scalaV)) | |
val settings = Seq( | |
version in ThisBuild := stampVersion, | |
// it's annoying that you have to do this to get the scalaVersion set for the subProjects | |
scalaVersion in ThisBuild := scalaV | |
) | |
append(settings, 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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On TimestampVersion.scala, I was having trouble with the scalaVersion not being correct without L44. This is the output that I was seeing before, you can see the sub-projects weren't getting the right version:
[info] Retrieving version from /web/commons-admin/target/timestamp: 0.2.8.20140125022405 [scalaVersion: 2.10.2]
[info] Setting version to 0.2.8.20140125022405 [scalaVersion: 2.10.2]
[info] Set current project to commons-admin (in build file:/web/commons-admin/)
[info] commons-admin-core/:scala-version
[info] 2.9.1
[info] commons-admin-play/:scala-version
[info] 2.9.1
[info] commons-admin/*:scala-version
[info] 2.10.2