Scala sbt: Multiple dependencies in sbt - scala

I am a new user to Scala, following the way to create a scala sbt project.
https://www.youtube.com/watch?v=Ok7gYD1VbNw
After adding
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
to build.sbt, and refreshed the project, I got this msg.
[warn] Multiple dependencies with the same organization/name but different versions. To avoid conflict, pick one version:
[warn] * org.scala-lang:scala-reflect:(2.11.2, 2.11.7)
[warn] * org.scala-lang.modules:scala-xml_2.11:(1.0.2, 1.0.4)
And in build.sbt, thw word 'scalatest' is red that means it's an unsolved dependencies.
Should I change something in Project Setting, like scala sdk?
Best Regard!

You could regard adding those dependencies:
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % "2.11.7",
"org.scala-lang.modules" % "scala-xml_2.11" % "1.0.4"
)
It forces compiler to choose concrete version of libraries. It solves problem for me.

I was able to resolve this by excluding these from the scalatest dependency.
libraryDependencies ++= Seq(
"org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
exclude("org.scala-lang", "scala-reflect")
exclude("org.scala-lang.modules", "scala-xml_2.11")
)

Related

How to define Scala API for Kafka Streams as dependency in build.sbt?

I am trying to start a new SBT Scala project and have the following in build.sbt file:
name := "ScalaKafkaStreamsDemo"
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1" artifacts(Artifact("javax.ws.rs-api", "jar", "jar"))
libraryDependencies += "org.apache.kafka" %% "kafka" % "2.0.0"
libraryDependencies += "org.apache.kafka" % "kafka-streams" % "2.0.0"
So according to the GitHub repo, in 2.0.0 I should see the Scala classes/functions etc etc that I want to use, however they just don't seem to be available. Within IntelliJ I can open up the kafka-streams-2.0.0.jar, but I don't see any Scala classes.
Is there another JAR I need to include?
Just while we are on the subject of extra JARs, does anyone know what JAR I need to include to be able to use the EmbeddedKafkaCluster?
The artifact you need is kafka-streams-scala:
libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % "2.0.1"
(please use 2.0.1, or even better 2.1.0, as 2.0.0 has some scala API bugs)
To answer your latter question, it's in the test-jar, which you can address using a classifier:
libraryDependencies += "org.apache.kafka" %% "kafka-streams" % "2.0.1" % "test" classifier "test"
But note that this is an internal class and subject to change (or removal) without notice. If at all possible, it's highly recommended that you use the TopologyTestDriver in the test-utils instead:
libraryDependencies += "org.apache.kafka" %% "kafka-streams-test-utils" % "2.0.1" % "test"
It looks like you face the issue of unresolved javax.ws.rs-api dependency that happens with some Java projects that are direct or transitive dependencies of Scala projects that use sbt. I've faced it with Scala projects that use Apache Spark and recently with Kafka Streams (with and without the Scala API).
A workaround that has worked fine for me is to simply exclude the dependency and define it again explicitly.
excludeDependencies += ExclusionRule("javax.ws.rs", "javax.ws.rs-api")
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1"
Make sure that you use the latest and greatest of sbt (i.e. 1.2.7 as of the time of this writing).
With that said, the dependencies in build.sbt should be as follows:
scalaVersion := "2.12.8"
val kafkaVer = "2.1.0"
libraryDependencies += "org.apache.kafka" % "kafka-streams" % kafkaVer
libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % kafkaVer
excludeDependencies += ExclusionRule("javax.ws.rs", "javax.ws.rs-api")
libraryDependencies += "javax.ws.rs" % "javax.ws.rs-api" % "2.1.1"
Within IntelliJ I can open up the kafka-streams-2.0.0.jar, but I don't see any Scala classes. Is there another JAR I need to include?
The following dependency is all you need:
libraryDependencies += "org.apache.kafka" %% "kafka-streams-scala" % kafkaVer
You can also use following workaround, that works in my case - more details
here
import sbt._
object PackagingTypePlugin extends AutoPlugin {
override val buildSettings = {
sys.props += "packaging.type" -> "jar"
Nil
}
}

Issue importing Akka packages

I have the following build.sbt file:
name := "akkaHttp"
version := "1.0"
scalaVersion := "2.12.0"
resolvers ++= Seq("Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
Resolver.bintrayRepo("hseeberger", "maven"))
libraryDependencies ++= {
val AkkaVersion = "2.3.9"
val AkkaHttpVersion = "2.0.1"
val Json4sVersion = "3.2.11"
Seq(
"com.typesafe.akka" %% "akka-slf4j" % AkkaVersion,
"com.typesafe.akka" %% "akka-http-experimental" % AkkaHttpVersion,
"ch.qos.logback" % "logback-classic" % "1.1.2",
"org.json4s" %% "json4s-native" % Json4sVersion,
"org.json4s" %% "json4s-ext" % Json4sVersion,
"de.heikoseeberger" %% "akka-http-json4s" % "1.4.2"
)
}
In it, these dependencies are not met.
Error:Unresolved dependencies:
com.typesafe.akka#akka-slf4j_2.12;2.3.9: not found
com.typesafe.akka#akka-http-experimental_2.12;2.0.1: not found
org.json4s#json4s-native_2.12;3.2.11: not found
org.json4s#json4s-ext_2.12;3.2.11: not found
de.heikoseeberger#akka-http-json4s_2.12;1.4.2: not found
So what should I add to it such that the imports work?
Notice the _2.12 appended to the artifacts? Based on your scalaVersion, SBT tried to download the dependencies built against Scala 2.12.x, but couldn't find any. Try using Scala version 2.11.8.
Different versions of Scala can be binary incompatible, so libraries are cross built against those different versions and published to the repositories. Looks like it hasn't yet happened for those libraries above.
Note that Scala 2.12 is indeed not binary compatible with Scala 2.11.

Why does sbt give multiple dependencies warning with Akka and ScalaTest dependencies?

I've just added ScalaTest to build.sbt so it now looks as follows:
name := "appname"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.1",
"org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
)
After that I am receiving the warning message:
SBT project import
[warn] Multiple dependencies with the same organization/name but different versions. To avoid conflict, pick one version:
[warn] * org.scala-lang:scala-reflect:(2.11.2, 2.11.7)
[warn] * org.scala-lang.modules:scala-xml_2.11:(1.0.2, 1.0.4)
I also tried changing the line concerning ScalaTest into:
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
but the warning still remains the same as above.
How could I deal with this issue since I haven't anywhere written "reflect" or "xml" in my project. I am using the newest version of both Akka and ScalaTest and Scala version 2.11.
The solution might be to add explicitly one of proposed versions by SBT. All warnings pass away when libraryDependencies is:
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.1",
"org.scalatest" %% "scalatest" % "2.2.4" % "test",
"org.scala-lang" % "scala-reflect" % "2.11.7",
"org.scala-lang.modules" %% "scala-xml" % "1.0.4"
)
In your particular case this is related to the ISSUE 1933 and you can ignore it for now. You can also explicitly specify version of the dependency you need, to silence warnings.
The problem is fixed with sbt 0.13.12

Play project compiles successfully but IDEA cannot resolve symbols while importing dependencies

Piece of build.sbt related to Play! application project.
resolvers += Resolver.jcenterRepo,
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
libraryDependencies ++= Seq(
"com.kyleu" %% "jdub-async" % "1.0",
"com.vmunier" %% "play-scalajs-scripts" % "0.3.0",
"org.webjars" % "jquery" % "1.11.1",
"org.json4s" %% "json4s-jackson" % "3.2.11",
evolutions,
"com.github.benhutchison" %% "prickle" % "1.1.7",
specs2 % Test
)
IDEA said to me that it cannot resolve symbol jdub when I import jdub-async library. Meanwhile project is compiling successfully.
How is it possible to fix this bug?
This is just an issue with IntelliJ IDEA's own Scala compiler. It chokes on many things. If it compiles fine with SBT itself, you don't have any bug to resolve (unless you want to report it to IntelliJ).

SBT fails to resolve dependency for jersey-container-grizzly2-http 2.5.1

I am fairly new to Scala and SBT. I would like to implement a REST server using Jersey JAX-RS and Scala, but have faced the first hurdle trying to configure SBT. I was planning to use the Grizzly HTTP Server. The build.sbt is as follows:
name := "scala-jersey-server"
version := "1.0"
scalaVersion := "2.10.3"
libraryDependencies += "org.glassfish.jersey.containers" % "jersey-container-grizzly2-http" % "2.5.1"
Running sbt update fails with the following messages
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.glassfish.hk2#hk2-utils;2.5.1: not found
[warn] :: org.glassfish.hk2#hk2-locator;2.5.1: not found
[warn] :: javax.validation#validation-api;${javax.validation.version}: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
Looking at the pom.xml for hk2-api, it shows dependency on hk2-utils using the ${project.version} variable, but I'd have expected version 2.2.0-b21 of hk2-utils to be requested by SBT not 2.5.1 (which is JAX-RS version).
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
<version>${project.version}</version>
</dependency>
Is this a problem with the configuration of hk2-api's pom.xml, is SBT using an incorrect version or something else?
I think there is an error in the publication chain of your dependency for the 2.5.1 version.
There are at least three possibilities:
1. Use another version
//http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.glassfish.jersey.containers%22%20AND%20a%3A%22jersey-container-grizzly2-http%22
libraryDependencies += "org.glassfish.jersey.containers" % "jersey-container-grizzly2-http" % "2.5"
2. Use another library
3. Exclude the broken dependencies and set working versions
Use http://search.maven.org/ to find existing versions.
libraryDependencies += "org.glassfish.jersey.containers" % "jersey-container-grizzly2-http" % "2.5.1" exclude("org.glassfish.hk2", "hk2-utils") exclude("org.glassfish.hk2", "hk2-locator") exclude("javax.validation", "validation-api")
//http://search.maven.org/#browse%7C703203458
libraryDependencies += "org.glassfish.hk2" % "hk2-utils" % "2.2.0-b27"
//http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22hk2-locator%22
libraryDependencies += "org.glassfish.hk2" % "hk2-locator" % "2.2.0-b27"
//http://search.maven.org/#artifactdetails%7Cjavax.validation%7Cvalidation-api%7C1.1.0.Final%7Cjar
libraryDependencies += "javax.validation" % "validation-api" % "1.1.0.Final"
**I tried the above .sbt file but it was not excluding the jars and was still giving the same ERROR.
So modified the build file. below sample it could create a build successfully.**
name := "KafkaSparkCouchReadWrite"
organization := "my.clairvoyant"
version := "1.0.0-SNAPSHOT"
scalaVersion := "2.11.11"
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "2.1.0",
"org.apache.spark" %% "spark-streaming" % "2.1.0",
"org.apache.spark" %% "spark-sql" % "2.1.0",
"com.couchbase.client" %% "spark-connector" % "2.1.0",
"org.glassfish.hk2" % "hk2-utils" % "2.2.0-b27",
"org.glassfish.hk2" % "hk2-locator" % "2.2.0-b27",
"javax.validation" % "validation-api" % "1.1.0.Final"
).map(_.excludeAll(ExclusionRule("org.glassfish.hk2"),ExclusionRule("javax.validation")))