Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup build.sbt with sbt-assembly plugin?

Tags:

java

scala

sbt

Posting this only because no other suggested solutions helped. I have a very simple one-class Scala sbt project which needs to be a runnable .jar file. It has some java dependencies so sbt package won't work for me, I need a fat jar. The project has the following structure.

├── build.sbt
├── project
│   ├── assembly.sbt
│   ├── build.properties
│   ├── project
│   └── target
├── README.md
├── src
│   ├── main
│   └── test
├── target
│   ├── scala-2.12
│   ├── specs2-reports
│   ├── streams
│   └── test-reports

It has one main class and one test spec. In order to make sbt generate a fat jar I added assembly.sbt into project dir and modified my build.sbt accordingly.

import AssemblyKeys._

name := "myproj"

version := "1.0"

scalaVersion := "2.12.8"

libraryDependencies ++= Seq(
  "au.com.bytecode" %% "opencsv" % "2.4",
  "org.specs2" %% "specs2-core" % "4.3.0" % "test"
)

scalacOptions in Test ++= Seq("-Yrangepos")

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

However,import AssemblyKeys._ is red with cannot resolve symbol? Trying it from console gives me

import AssemblyKeys._
       ^
[error] Type error in expression
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? 

What else have I forgotten because assebly github page does not state that we need to do anything else. My sbt version is sbt 1:1.2.8-1.

like image 589
minerals Avatar asked Feb 21 '19 13:02

minerals


People also ask

What is sbt assembly?

The sbt-assembly plugin is an SBT plugin for building a single independent fat JAR file with all dependencies included. This is inspired by the popular Maven assembly plugin, which is used to build fat JARs in Maven.

What is a sbt plugin?

A plugin can define a sequence of sbt settings that are automatically added to all projects or that are explicitly declared for selected projects. For example, a plugin might add a proguard task and associated (overridable) settings. Finally, a plugin can define new commands (via the commands setting).


1 Answers

Under your project directory, you need to add a plugins.sbt file which has the following included:

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

Then, delete AssemblyKeys._ and refresh sbt. Only after a successful refresh you'll be able to insert that import

like image 77
Yuval Itzchakov Avatar answered Oct 10 '22 17:10

Yuval Itzchakov