【Scala】SBT ~ sbtプラグイン ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/03/22/000000 https://dk521123.hatenablog.com/entry/2023/01/27/000000 https://dk521123.hatenablog.com/entry/2023/01/26/000000

の続き。

徐々に、SBT について分かってきた。
今回は、sbtプラグイン について触れる。
これで、JARファイル作成ができるし、フォーマッターにも掛けられる
など、いいことづくめ、、、

目次

【1】sbtプラグイン
 1)sbtプラグイン一覧
【2】sbtプラグインの導入の仕方
 1)plugins.sbt
【3】主なsbtプラグイン
 1)sbt-assembly
 2)sbt-scalafmt
 3)scalastyle-sbt-plugin
 4)sbt-scoverage / sbt-jacoco
 5)sbt-pack

【1】sbtプラグイン

* SBT を使う際の便利ツール
 => 詳細は以下。

https://www.scala-sbt.org/1.x/docs/ja/Using-Plugins.html

1)sbtプラグイン一覧

* 以下から欲しいプラグインを見つけるといいかも。

https://www.scala-sbt.org/1.x/docs/Community-Plugins.html

【2】sbtプラグインの導入の仕方

https://dk521123.hatenablog.com/entry/2023/03/22/000000

├─build.sbt ... ビルド定義ファイル
├─project
│  ├─build.properties ... sbtバージョン設定
│  └─plugins.sbt ... sbtプラグインの設定 << ★ここに記載する

1)plugins.sbt

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "<バージョン>")


https://github.com/jhole89/aws-glue-sbt-quickstart/blob/master/project/plugins.sbt

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.2.1")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")

【3】主なsbtプラグイン

1)sbt-assembly

* jar生成するためのプラグイン

https://github.com/sbt/sbt-assembly

* 詳細な使い方は、以下の関連記事を参照のこと

SBT ~ JAR作成/実行 ~
https://dk521123.hatenablog.com/entry/2023/10/12/200450

追加例:plugins.sbt

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

build.sbt 記述例

// assemblyJarName := "【JARファイル名】"

sbtコマンド

// 以下でJAR作成
sbt assembly

参考文献
https://qiita.com/suin/items/b8a7af13b00cfdecfd1e
https://qiita.com/Setz/items/d060c129940bae1abdac
https://qiita.com/TakashiOshikawa/items/2a9f4f7a0c88e9629413

2)sbt-scalafmt

* Scala の コードフォーマッタ

追加例:plugins.sbt

// In project/plugins.sbt. Note, does not support sbt 0.13, only sbt 1.x.
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0")
// or
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.5.1") // before 1.6.0-RC4

コマンド一覧

# コマンド 説明
1 scalafmt ソースコード(main)をフォーマット
2 test:scalafmt テストコード(test)をフォーマット
3 scalafmtSbt .sbt および project/.scala ファイルをフォーマット
4 scalafmtCheck フォーマッタのルールに従っているかチェック
5 scalafmtSbtCheck .sbt および project/.scala ファイルのフォーマットをチェック

参考文献
https://dev.classmethod.jp/articles/format-with-scalafmt/

3)scalastyle-sbt-plugin

* Scala のコーディングスタイルチェッカー

https://github.com/scalastyle/scalastyle-sbt-plugin
http://www.scalastyle.org/sbt.html

追加例:plugins.sbt

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")

sbtコマンド

sbt scalastyle

参考文献
https://qiita.com/meganemura/items/b09709d3b05e0c0a93dd

4)sbt-scoverage / sbt-jacoco

* コードカバレッジ(code coverage)
* 詳細は、以下の関連記事を参照のこと

ScalaTest ~ with Coverage ~
https://dk521123.hatenablog.com/entry/2023/08/07/222945

5)sbt-pack

https://github.com/xerial/sbt-pack

より抜粋
~~~~
A sbt plugin for creating distributable Scala packages
 that include dependent jars and launch scripts.

[意訳]
依存しているJARおよびスクリプト起動を含んだ
配布用Scalaパッケージを作成するSBTプラグインです
~~~~

追加例:plugins.sbt
https://index.scala-lang.org/xerial/sbt-pack/artifacts/sbt-pack/0.7.4

addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.7.4")

sbtコマンド

sbt pack

関連記事

Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331
SBT ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/22/000000
SBT ~ 基本編 / build.sbt ~
https://dk521123.hatenablog.com/entry/2023/01/27/000000
SBT ~ 基本編 / sbtコマンド ~
https://dk521123.hatenablog.com/entry/2023/01/26/000000
SBT ~ JAR作成/実行 ~
https://dk521123.hatenablog.com/entry/2023/10/12/200450
SBTでのエラー/警告対応
https://dk521123.hatenablog.com/entry/2023/04/06/093458
ScalaTest ~ with Coverage ~
https://dk521123.hatenablog.com/entry/2023/08/07/222945
Github Actions ~ Scala Linter ~
https://dk521123.hatenablog.com/entry/2024/04/02/002828