SBT: modify the order of dependencies in the classpath - scala

I'm currently experiencing a problem with Specs2 + SBT where my tests always fail via command-line because of dependency order in the classpath. Specs2 requires that the Mockito jars come after the Specs2 jars so that Mockito classes can be overridden to fix issues with by-name scala method parameters (see this issue for more information: https://github.com/etorreborre/specs2/issues/428).
In IntelliJ, I can order my dependencies via the Project Structure/Modules/Dependencies window, which fixes my tests when run inside IntelliJ, however, I have not found a solution to fix this issue when running my tests on the command-line via sbt test.
Does anyone know if it is possible to change the classpath order of dependencies for SBT using settings in build.sbt (or similar)?

To my knowledge you need to make sure that specs2-mock comes before mockito in your libraryDependencies setting.

Related

Intellij sbt won't resolve dependencies' dependencies

I have a Intellij Scala project, that depends on another lib, which in turn depends on some other libs (gson, log4j...), but somehow Intellij won't add those transitive dependencies to my project, so when I run my main method, jvm always complains no class found. I also tried sbt update and sbt update-classifier, they won't resolve down the dependency path as well. How can I have sbt resolve all levels of dependencies?
If all works in SBT command lines but not in Idea then remove the .idea folder and reload the SBT project in Idea.
I should warn that you will loose the project configs (such as run configs) by doing that.
I finally figure out what was wrong, it was the sbt-s3-resolver plugin I was using couldn't resolve maven style dependencies, I used an another plugin which supports maven libs and it worked. But I still couldn't figure out why it worked before.

Why don't specs2 + scalamock tests run in IntelliJ? Multiple suite traits detected

I'm trying to run Specs2 tests from IDEA from both Windows and Mac versions of Intellij IDEA 14. I generated the idea project files using both gen-idea and the built in SBT plugin and get the same results...
When I try to run them, I get
Error running Specs2 in 'scala': Test class not found
I've added packages and switched "search for tests" to in single module but still no avail. I also get the message below on the run config.
Multiple suite traits detected: List(ScTrait: SpecificationStructure, ScTrait: SpecificationStructure,)
The similar post How to run all Specs2 tests under IntelliJ IDEA? doesn't help and it all runs fine from SBT, BTW. I can't run individual tests via the short-cut either :'(
I'm suspecting it's a combination of scalamock and specs2, as if I remove the following from my build.sbt, I can run them again.
"org.scalamock" %% "scalamock-specs2-support" % "3.2" % "test"
Here's the run config.
You should check if you have multiple specs2 jars dependencies in your module. If you just leave one jar dependency the configuration error should disappear.
To help other googlers, it could also be that you have one package depending on specs2-core, and scalamock-specs2-support which depends on specs2. The solution is to add an explicit dependency on specs2, and both dependencies should be evicted to the one you import. I was just able to fix this with the following dependency: "org.specs2" %% "specs2" % "3.7" % "test"

Real SBT Classpath at Runtime

I have some test cases that need to look at the classpath to extract the paths of some files/directories in there. This works fine in the IDE.
The problem is that, when running SBT test, Properties.javaClassPath gives me /usr/share/sbt-launcher-packaging/bin/sbt-launch.jar.
The classpath is fine when I run show test:dependency-classpath. Is there a way to obtain that information from inside the running Scala/Java program? Or is there a way to toss it into a system property or environment variable?
By default the tests are run inside of the SBT process, so the classpath will look like it did when you started sbt (I guess sbt does some trixery to dynamicly load the classes for the tests, not sure). One way to do what you want is to run your tests in a forked jvm, that way sbt will start a new jvm to run the test suite and that should have the expected class path:
fork in Test := true
I have been working on understanding how the EmbeddedCassandra works in the spark-cassandra-connector project which uses the classpath to start up and control a Cassandra instance. Here is a line from their configuration that gets the correct classpath.
(compile in IntegrationTest) <<= (compile in Test, compile in IntegrationTest) map { (_, c) => c }
The entire source can be found here: https://github.com/datastax/spark-cassandra-connector/blob/master/project/Settings.scala
Information on the <<= operator can be found here: http://www.scala-sbt.org/0.12.2/docs/Getting-Started/More-About-Settings.html#computing-a-value-based-on-other-keys-values. I'm aware that this is not the current version of sbt, but the definition still holds.

SBT doesn't reconize junit testcase written in java file

I made a Scala/Java mixed project with SBT 0.11.2. My config for JUnit testing is
resolvers += "twitter.com" at "http://maven.twttr.com/"
seq(com.github.retronym.SbtOneJar.oneJarSettings: _*)
libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M2" % "test"
When I write JUnit test cases in Scala with #Test, every goes well. But when I write a Java JUnit test case, then run test in sbt, the Java JUnit test cannot be reconized. Only test cases written in Scala are executed.
How can I make sbt recognize my Java and Scala test cases at the same time?
Probably late for the origional question, but..
I've just been looking at this. The JUnit tests in my project were not running for me until I ran sbt clean test. Now all working like a charm.
There was a bug in 0.11.x in detecting Java tests that was fixed in 0.12.0, although I didn't think it affected detecting annotated tests. You might try coming up with a minimal test case and checking with the latest sbt version (0.12.1). If the problem still exists, file a bug.
you should put your test classes into src/test/java and your class name should end with "Test" (for example myTest.java)

Maven SBT dependency artifacts?

I manage my project using Maven and SBT at same time. The reasons for this are:
Intellij IDEA cannot import SBT
project.(idea-sbt plugin doesn't
work very well)
I don't know how can get sources and
javadocs from SBT.(I'd like to see any answers about this)
The problem is I don't know how to let Maven download SBT dependency. I search through maven repository and couldn't find anything about sbt. I wanna use Maven or SBT to manage all the jars in my project.
If you put a pom.xml to the root of your project, it will be recognized by SBT. When you specify no managed dependencies in the project definition, SBT relies on Maven dependencies.
As it said in SBT doumentation,
sbt performs this dependency handling
when the update action is executed. By
default, sbt does not update your
dependencies before every compilation,
but only does so when you execute
update. sbt supports three ways of
specifying these dependencies:
* Declarations in your project definition
* Maven POM files
* Ivy configuration and settings files
Maven knows nothing about SBT as of now (at least, I've not heard about any plugins so far), so, the best you can do to manage your project both in Maven and SBT, is to generate POMs by SBT. See SBT to Maven Converter for more details.
idea-sbt plugin works great for me with IDEA 10 - all it's really intended to do is open an SBT shell within the IDE and it does that well enough.
A plugin you should look into if you're interested in getting the Maven out of your build is sbt-idea plugin ( https://github.com/mpeltonen/sbt-idea ). This is a great plugin that generates IDEA files from an SBT project. It couldn't be easier to use. At an SBT prompt, run the following commands:
*sbtIdeaRepo at http://mpeltonen.github.com/maven/
*idea is com.github.mpeltonen sbt-idea-processor 0.3.0
update
idea
Note the asterisks - they should be included.
At this point, you can open your project in IDEA. It won't complain about the SBT dependencies. Any time you add new dependencies to your project file, simply run the 'idea' command again to tell IDEA about it. I do that in the SBT window provided by idea-sbt.
As far as getting sources and docs with dependencies, you can do something like this (from the SBT docs):
val sc = "org.scalacheck" % "scalacheck" % "1.5" withSources()
There is a corresponding withJavadoc() method. Hope that helps.