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

build.sbt の中で application.conf を読み込む

More than 3 years have passed since last update.

build.sbt の中で application.conf の内容を使いたいことがあります。例えば flyway-sbt の flywayUrl や flywayUser は、アプリケーションで使っている設定と同じものを使いたいことが多いと思います。

このような場合、普通に build.sbt の中で

build.sbt
...

libraryDependencies ++= Seq(
  ...
  libraryDependencies += "com.typesafe" % "config" % "1.2.1"
)

import com.typesafe.config.ConfigFactory

...

としても com.typesafe.config.ConfigFactory が見つからないよと言われてしまいますが、

project ディレクトリの中に foobar.sbt という下の内容のファイルを作れば、build.sbt の中で com.typesafe.config をインポートできるようになります。

project/config.sbt
libraryDependencies += "com.typesafe" % "config" % "1.2.1"

例えば、

build.sbt
import com.typesafe.config.ConfigFactory

val conf = ConfigFactory.parseFile(new File("src/main/resources/application.conf"))

flywayUrl := conf.getString("db.default.url")

flywayUser := conf.getString("db.default.user")

こんなかんじにすれば application.conf を読み込んで使えるようになります (ConfigFactory.load() では読み込んでくれなかったので ConfigFactory.parseFile(..) を使っています)。

$ cat src/main/resources/application.conf
db.default.url="jdbc:h2:file:default"
db.default.user="sa"
...
$ sbt
> show flywayUrl
[info] jdbc:h2:file:default

ちゃんと読み込まれています。

参考: http://stackoverflow.com/questions/15425609/sbt-cant-resolve-dependency-in-build-definition

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
ユーザーは見つかりませんでした