LoginSignup
41
32

More than 5 years have passed since last update.

sbt-assembly(0.12.0)を使ってみる

Posted at

sbt 0.13.6以前は plugin.sbt

plugin.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")

を追加して、

build.sbt

buid.sbt
import AssemblyKeys._
assemblySettings

を追加するみたい。

だけど、 sbt 0.13.6 からはauto pluginのようなので基本的には

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

を追加するだけでいいみたい。

使い方

project/assembly.sbt にpluginを追加する。

project/assembly.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")

sbt assembly実行時に重複したファイル名を参照している部分があると、途中でjava.lang.RuntimeException: deduplicate: different file contents found in the followingが出てエラーになってしまうので、 build.sbtMerge Strategyを追加する。

build.sbt
assemblyMergeStrategy in assembly := {
  case PathList("javax", "servlet", xs @ _*)         => MergeStrategy.first
  case PathList(ps @ _*) if ps.last endsWith ".properties" => MergeStrategy.first
  case PathList(ps @ _*) if ps.last endsWith ".xml" => MergeStrategy.first
  case PathList(ps @ _*) if ps.last endsWith ".types" => MergeStrategy.first
  case PathList(ps @ _*) if ps.last endsWith ".class" => MergeStrategy.first
  case "application.conf"                            => MergeStrategy.concat
  case "unwanted.txt"                                => MergeStrategy.discard
  case x =>
    val oldStrategy = (assemblyMergeStrategy in assembly).value
    oldStrategy(x)
}

あとはprojectのroot directoryでsbt assemblyすると無事successになる。

ハマリポイント

例として下記のようなerrorが出る場合、 io.netty.versions.properties の重複が問題になっているので、Merge Strategyにcase PathList(ps @ _*) if ps.last endsWith ".properties" => MergeStrategy.firstなどのようにルールを加える必要がある。

[error] (*:assembly) deduplicate: different file contents found in the following:
[error] /<your_home>/.ivy2/cache/io.netty/netty-all/jars/netty-all-4.0.24.Final.jar:META-INF/io.netty.versions.properties
[error] /<your_home>/.ivy2/cache/io.netty/netty-tcnative/jars/netty-tcnative-1.1.30.Fork2-linux-x86_64.jar:META-INF/io.netty.versions.properties
[error] /<your_home>/.ivy2/cache/io.netty/netty-tcnative/jars/netty-tcnative-1.1.30.Fork2-windows-x86_64.jar:META-INF/io.netty.versions.properties
[error] /<your_home>/.ivy2/cache/io.netty/netty-tcnative/jars/netty-tcnative-1.1.30.Fork2-osx-x86_64.jar:META-INF/io.netty.versions.properties

Merge Strategyを記述して問題を解決しましたが、もしかしてMerge Strategyを書かなくても解決できる??

41
32
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
32