Help us understand the problem. What is going on with this article?

scala実行コンテキストでbuild.sbtの情報を使う

More than 3 years have passed since last update.

Goal

  • scalaバッチを頻繁にリリースすると、今動いてるバージョンの確信が持てなくなるので、バッチのはじめにアプリケーションバージョンをログ出力したい
  • そのためにscala実行コンテキストでbuild.sbtに記述されるversionを取得したい

Solution

sbt plugin: sbt-buildinfoが有効.
https://github.com/sbt/sbt-buildinfo

project/buildinfo.sbt
# sbtVersion 0.13.6+の場合その他のバージョンはgithub README参照
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
build.sbt
lazy val root = (project in file(".")).
  enablePlugins(BuildInfoPlugin).
  settings(
    buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
    buildInfoPackage := "hello"
  )

この設定でコンパイルするとpackage hello下にBuildInfoクラスが含まれる.
sbt compileしたあと、以下のパスからソースも確認できる.
target/scala-2.11/src_managed/main/sbt-buildinfo/BuildInfo.scala

Use Example

Main.scala
package jp.metheglin

import hello.BuildInfo

object Main {
  def main(args: Array[String]) {
    val logger = Logger(LoggerFactory.getLogger(this.getClass))
    // BuildInfo.toStringで一通りの情報が返る
    // 個別の情報はBuildInfo.versionのように取得する
    logger.info("Application Build: " + BuildInfo.toString)
  }
}

実行結果

2016-12-06 00:40:03.590 INFO  jp.metheglin.Main$ - Application Build: name: test-batch, version: 2.2.2, scalaVersion: 2.11.8, sbtVersion: 0.13.8

Environment

sbt sbtVersion
  # [info] 0.13.8

scala -version
  # Scala code runner version 2.11.7 -- Copyright 2002-2013, LAMP/EPFL
metheglin
敷布団カバーと掛布団カバーを逆につけて寝ています
https://metheglin.jp/#logical
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした