installing sbt-assembly with sbt 0.11.2 - scala

I am trying to install sbt-assembly by following the instructions in order to make a stand-alone jar that can run on a computer without scala installed.
So far these are the steps I've taken.
I created a plugins.sbt file:
$ cat sbt/project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
And I added the following to the beginning of my build.sbt file:
$ head -n3 sbt/build.sbt
import AssemblyKeys._ // put this at the top of the file
seq(assemblySettings: _*)
But when I run sbt, I get the following error:
sbt/build.sbt:1: error: not found: value AssemblyKeys
import AssemblyKeys._

Make sure you are running sbt version at least 0.11 by typing
$ sbt sbt-version
at the bash prompt.
Make sure you have the plugins file set up as follows:
$ cat sbt/project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
Make your build file (build.sbt) look like this:
import AssemblyKeys._
seq(assemblySettings: _*)
name := "my_project"
version := "1.0"
scalaVersion := "2.9.1"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "1.6.1" % "test",
"commons-lang" % "commons-lang" % "2.6"
)
traceLevel in run := 0
fork in run := true
scalacOptions ++= Seq("-optimize")
// The following is the class that will run when the jar is compiled!
mainClass in assembly := Some("MyMain")

Make sure you don't have a project/plugins folder lying around. This may prevent other mechanisms of specifying plugins from working.

You shouldn't import plugin settings into build.sbt (basic configuration): 1) build.sbt is not a normal Scala source file 2) plugin settings are pre-imported.
So you simply should do
seq(assemblySettings: _*)
Imports are required only when you use full/extended build configuration.

Related

Exception in thread "main" java.lang.NoClassDefFoundError: org/rogach/scallop/ScallopConf

As I want to execute the jar generated by my scala project in the Command Line Interface, I get the following problem:
Exception in thread "main" java.lang.NoClassDefFoundError: org/rogach/scallop/ScallopConf
Although in the dependency file I mentionned scallop dependency as follow
import sbt._
object Dependencies {
lazy val betterFiles = "com.github.pathikrit" %% "better-files" % "3.7.0"
lazy val scalaz = "org.scalaz" %% "scalaz-core" % "7.2.27"
lazy val scallop = "org.rogach" %% "scallop" % "3.1.5"
// -- Logging
lazy val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
lazy val slf4jBackend = "org.slf4j" % "slf4j-simple" % "1.7.26"
// -- Testing
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
My build.sbt file is the following:
import Dependencies._
ThisBuild / scalaVersion := "2.12.5"
ThisBuild / sbtVersion := "1.2.6"
ThisBuild / version := "0.1.0-SNAPSHOT"
lazy val root = (project in file("."))
.settings(
name := "phenix-challenge",
libraryDependencies ++= Seq(
betterFiles,
scalaz,
scallop,
scalaLogging,
slf4jBackend % Runtime,
scalaTest % Test
)
)
If you have an Idea that could resolve my Issue please HELP!
Many thanks in advance
To execute the jar generated by your scala project in the Command Line Interface you can use sbt plugin to assembly a fat-jar including your libraries/dependencies. Having such jar you would be able to run your app via java -jar ...
There are several SBT plugins for build a fat-jar. Perhaps the easiest one would be the sbt-assembly.
Add this plugin to file project/plugins.sbt (create this file if needed):
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")
Now use sbt to build a fat-jar:
sbt assembly
Then run via java -jar YouMainClass
Another option is to use pure sbt to run Main class using command sbt run, then you do really need to build a fat-jar.

Not able to import Specs2 in eclipse (scala-ide) project

Kindly consider if there's something very stupid I am missing, I am very new to this.
Creating this example on Eclipse (Scala-ide). When I create the CorrelationJobTest.scala class, there's a problem in importing classes from specs2 package.
import org.specs2.runner.JUnitRunner
The error I am getting is pretty obvious,
object specs2 is not a member of package org
Even after creating a build.sbt file, adding the dependencies, and doing a sbt clean, the error is still showing.
Below is my build.sbt
name := "SparkCorrelation"
organization := "com.xyz"
version := "0.1"
/* scala versions and options */
scalaVersion := "2.10.4"
// These options will be used for *all* versions.
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation")
scalacOptions ++= Seq("-feature", "-unchecked", "-deprecation")
/* dependencies */
libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.7" % "test")
scalacOptions in Test ++= Seq("-Yrangepos")
EDIT:
I faced this problem with apache.spark packages too. Then I included the jar spark-assembly-1.6.0-hadoop-2.6.0.jar and it got solved. Isn't there a library for specs2 that I can import in Eclipse?
Got it working. Here's what I did:
Deleted the project from eclipse
Did sbt eclipse
Imported the project into eclipse again.
Added dependencies in build.sbt
Did sbt clean
The problem was that the project was directly created in eclipse and was not able to link to sbt build file.
You need to add the junit module to your dependencies
libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "3.7" % "test",
"org.specs2" %% "specs2-junit" % "3.7" % "test")

Cannot run jar file created from Scala file

This the code that I have written in Scala.
object Main extends App {
println("Hello World from Scala!")
}
This is my build.sbt.
name := "hello-world"
version := "1.0"
scalaVersion := "2.11.5"
mainClass := Some("Main")
This is the command that I have run to create the jar file.
sbt package
My problem is that a jar file named hello-world_2.11-1.0.jar has been created at target/scala-2.11. But I cannot run the file. It is giving me an error saying NoClassDefFoundError.
What am I doing wrong?
It also says what class is not found. Most likely you aren't including scala-library.jar. You can run scala target/scala-2.11/hello-world_2.11-1.0.jar if you have Scala 2.11 available from the command line or java -cp "<path to scala-library.jar>:target/scala-2.11/hello-world_2.11-1.0.jar" Main (use ; instead of : on Windows).
The procedure depicted proves valid up to the way the jar file is executed. From target/scala-2.11 try running it with
scala hello-world_2.11-1.0.jar
Check whether it is runnable also from the project root folder with sbt run.
To run the jar file(containing scala code) with multiple main classes use following approach
scala -cp "<jar-file>.jar;<other-dependencies>.jar" com.xyz.abc.TestApp
This command will take care of including scala-library.jar in dependency and will also identify TestApp as main class if it has a def main(args:Array[String]) method. Please note that multiple jar files should be separated by semi-colon(";")
We can use sbt-assembly to package and run the application.
First, create or add the plugin to project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")
The sample build.sbt looks like below:
name := "coursera"
version := "0.1"
scalaVersion := "2.12.10"
mainClass := Some("Main")
val sparkVersion = "3.0.0-preview2"
val playVersion="2.8.1"
val jacksonVersion="2.10.1"
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-library" % scalaVersion.toString(),
"org.apache.spark" %% "spark-streaming" % sparkVersion,
"org.apache.spark" %% "spark-core" % sparkVersion,
"org.apache.spark" %% "spark-sql" % sparkVersion,
"com.typesafe.play" %% "play-json" % playVersion,
// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-10
"org.apache.spark" %% "spark-streaming-kafka-0-10" % sparkVersion,
// https://mvnrepository.com/artifact/org.mongodb/casbah
"org.mongodb" %% "casbah" % "3.1.1" pomOnly(),
// https://mvnrepository.com/artifact/com.typesafe/config
"com.typesafe" % "config" % "1.2.1"
)
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs # _*) => MergeStrategy.discard
case x => MergeStrategy.first
}
From console, we can run sbt assembly and the jar file gets created in target/scala-2.12/ path.
sbt assembly will create a fat jar. Here is an excerpt from the documentation :
sbt-assembly is a sbt plugin originally ported from codahale's assembly-sbt, which I'm guessing was inspired by Maven's assembly plugin. The goal is simple: Create a fat JAR of your project with all of its dependencies.

how to configure mahout with play framework

How can I include mahout 0.9 libraries in play-framework 2.2.
I have added the jar files in the build path of eclipse but when I run the play app the following error is displayed for mahout imports
error: package org.apache.mahout.cf.taste.model does not exist
Include this line in build.sbt of play project
"org.apache.mahout" % "mahout-core" % "0.9"
Full build.sbt looks like this
name := <your_project_name>
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
"org.apache.mahout" % "mahout-core" % "0.9"
)
play.Project.playJavaSettings

How to set up managed dependencies in an SBT 0.11 project having Build.scala

I am building a simple Scala project with SBT 0.11.
All the code files are in ~/MyProject/src/main/scala/
~/MyProject/build.sbt is the following
name := "MyProject"
version := "1.0"
scalaVersion := "2.9.1"
libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.+",
"c3p0" % "c3p0" % "0.9.1.2",
"org.apache.commons" % "commons-lang3" % "3.0.1",
"commons-lang" % "commons-lang" % "2.6",
"javassist" % "javassist" % "3.12.1.GA"
)
~/MyProject/project/Build.scala is the following
import sbt._
object MyProjectBuild extends Build {
lazy val MyProject = Project("MyProject", file("."))
}
This seems to work almost fine. The project does compile and run. The project name is set correctly (if I don't use Build.scala, then the name seems to appear something like "default-????", despite it being specified in build.sbt).
But the problem is that dependencies do not seem to work - update command doesn't download anything. How to fix this? Do I need to specify my dependencies in Build.scala rather than in build.sbt in this case?
Is it possible that you've already retrieved the project dependencies, but don't realize it because they are stored in the Ivy cache? You can view the managed classpath from the SBT console with the command
show managed-classpath
Recent versions of SBT do not store the managed dependencies in the project directory, unless the project is configured to do so. If you want, you can add the following to your build.sbt file:
retrieveManaged := true
This will create a ~/MyProject/lib_managed/ directory and contents.