Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@metheglin

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

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
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
2
Help us understand the problem. What are the problem?