LoginSignup
3
4

More than 5 years have passed since last update.

sbt-git と sbt-buildinfo を使って、アプリケーションから git の commit hash を取得

Posted at

今日のアドカレ書いてないのに、そのものずばりなエントリがなさそうだったので記録

  • Scala 2.12.7
  • sbt 1.2.6
plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")
build.sbt
lazy val root = (project in file("."))
  .enablePlugins(BuildInfoPlugin, GitPlugin)
  .settings(
    buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion,
      "gitSha" -> git.gitHeadCommit.value.getOrElse(""),
      "gitBranch" -> git.gitCurrentBranch.value
    ),
    buildInfoPackage := "hoge"
  )

ビルドすると hoge.BuildInfoができるのでアプリケーションから参照できる

Main.scala
import hoge.BuildInfo

object Main {
  def main(args: Array[String]) {
    println(s"gitBranch: ${BuildInfo.gitBranch}, gitSha: ${BuildInfo.gitSha}")
  }
}

古えの時代には、sbt Taskでgitコマンドを叩く荒業をやっていたこともあった

あの頃の私は、もう戻らない

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4