■ はじめに
業務で、Scala の Resources を動的に切り替える必要があるので Scala の Resources について、色々調べたことをまとめる
目次
【1】Scala の Resources ディレクトリ 1)デフォルトのResources パス 【2】Resources 関連のAPI 【3】Resources あれこれ 0)使用上の注意 1)Resources のパス変更 2)Resources ディレクトリの追加 3)Resources の追加・除外 【4】サンプル 例1:TOMLファイル取得
【1】Scala の Resources ディレクトリ
* ソースコード以外のリソース置き場 + 例えば、設定ファイルなど置く等
ディレクトリ構成例
project-root
+ src
+ main
+ scala
+ resources << ★ここに設定ファイルなどを置く
1)デフォルトのResources パス
より抜粋
[1] Normal
src/main/resources
[2] Test
src/test/resources
【2】Resources 関連のAPI
* getClass.getResource("【ファイル】") でパス取得
* getClass.getResourceAsStream("【ファイル】") だとストリームとして取得
【3】Resources あれこれ
0)使用上の注意
* 下記1)以降を実行する前に「sbt clean」を行っておいた方が無難 => っというのも「2)Resources ディレクトリの追加」を行った時に 意図した動きにならずに、1日中ハマったため、、、
1)Resources のパス変更
* 以下の公式ドキュメントに記載
Compile / resourceDirectory := baseDirectory.value / "aws-resources" Test / resourceDirectory := baseDirectory.value / "test-resources"
2)Resources ディレクトリの追加
https://www.scala-sbt.org/1.x/docs/Howto-Customizing-Paths.html#Add+an+additional+resource+directory
Compile / unmanagedResourceDirectories += baseDirectory.value / "extra-resources" Test / unmanagedResourceDirectories ++= { Seq(baseDirectory.value / "extra-test-resources") }
3)Resources の追加・除外
# 除外 unmanagedResources / excludeFilter := HiddenFileFilter || "*impl*" # 追加 Compile / unmanagedResources / includeFilter := "*.txt" Test / unmanagedResources / includeFilter := "*.html"
【4】サンプル
例1:TOMLファイル取得
以下の関連記事のコードをベースに実装してみた
Scala ~ TOML ~
https://dk521123.hatenablog.com/entry/2023/10/08/232146
import com.electronwill.nightconfig.core.file.FileConfig import java.io.File object Main { def main(args: Array[String]): Unit = { // WindowsだとFileでワンクッション置く必要がある val configPath = new File(getClass.getResource("config.toml").getPath) val config = FileConfig.of(configPath) config.load() println("Config: " + config) println("title: " + config.get("title")) val server = config.getOptional("database.server") println("server: " + server.get()) val data = config.getOptional("clients.data") println("data: " + data.get()) config.close() println("Done!") } }
src/main/resources/config.toml
# This is a comment
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # date type
[database]
server = "127.0.0.1"
ports = [ 8080, 8081, 8082 ]
connection_max = 5000
enabled = true
[servers]
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ]
hosts = [
"alpha",
"omega"
]
# テーブルの配列
#
[[table_array]]
key1 = "value1"
build.sbt
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.14"
libraryDependencies ++= Seq(
"com.electronwill.night-config" % "toml" % "3.6.6",
)
lazy val root = (project in file("."))
.settings(
name := "sample"
)
関連記事
Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331
Scala ~ TOML ~
https://dk521123.hatenablog.com/entry/2023/10/08/232146
SBT ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/22/000000
SBT ~ 基本編 / build.sbt ~
https://dk521123.hatenablog.com/entry/2023/01/27/000000
SBT ~ JAR作成/実行 ~
https://dk521123.hatenablog.com/entry/2023/10/12/200450
SBT ~ 引数設定の仕方 ~
https://dk521123.hatenablog.com/entry/2023/08/21/000000
SBT ~ 設定の切替 ~
https://dk521123.hatenablog.com/entry/2023/08/28/165323