rwijngaa February 2016

sbt different libraryDependencies in test than in normal mode

Because of conflicting / transitive (elasticsearch / lucene / jackrabbit) dependencies i want to have different libraryDependencies in test than i have when normally running the app. I solved it with the setup below, but this requires running activator with -Dtest and this will prevent my app from running normally when i'm done testing. The other way around, i.e. running just activator, will run my app but will not run my test. So, not very convenient and i think this can be done much better (btw i'm very new to sbt/scala)

name := """example"""

version := "0.1"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.1"

// fork in Test := true

javaOptions in Test += "-Dconfig.file=conf/application.test.conf"

javaOptions in Test += "-Dlogger.file=conf/test-logger.xml"

// run activator -Dtest
if (sys.props.contains("test")) {
  Seq[Project.Setting[_]](  
    libraryDependencies ++= {
      Seq(
          javaJdbc,
          javaEbean,
          cache,
          javaWs,
          "org.webjars" %% "webjars-play" % "2.3.0-2",
          "org.webjars" % "bootstrap" % "3.3.6",
          "org.webjars" % "font-awesome" % "4.5.0",
          "be.objectify" %% "deadbolt-java" % "2.3.3",
          "org.apache.lucene" % "lucene-core" % "3.6.0",
          "org.elasticsearch" % "elasticsearch" % "1.7.4" exclude("org.apache.lucene", "lucene-core"),
          "javax.jcr" % "jcr" % "2.0",
          "org.apache.jackrabbit" % "jackrabbit-core" % "2.11.2",
          "org.apache.jackrabbit" % "jackrabbit-jcr2dav" % "2.11.2",
          "org.apache.tika" % "tika-parsers" % "1.11",
          "org.apache.tika" % "tika-core" % "1.11",
          "commons-io" % "commons-io" % "2.4",
          "com.typesafe.akka" %  "akka-testkit_2.11" % "2.3.14" % "test"
        )
    }
  )
} else {
  Seq[Project.Setting[_]](
   libraryDependencies ++= {
      Seq(
          javaJdbc,
          javaEbean,
          cache,
          javaWs,
          "        

Answers


dth February 2016

I don't have a setup where I can really test whether this works, but from how I understand sbt dependencies it should:

Dependencies can have a kind of scope called a configuration. Typically, this is used to define test only dependencies:

"com.typesafe.akka" %  "akka-testkit_2.11" % "2.3.14" % "test"

But you should also be able to define compile time and run time only dependencies using "compile" and "runtime" instead.

sbt prints me a warning if I use dependencies with different versions. The problem is, that this will use a different version of a dependency to compile it and then to run it with tests. So it will be run against a different version than it was compiled with. There are of course libraries, where this will work, especially, if you run with a newer version that what you use to compile.

If you really need to compile your application twice with different dependencies and use one build for running and one for testing, I fear, there won't be a solution without extending sbt or something like that.

You could try to make two modules, one with the main code and one for testing and then try to cross-build two different versions of the first module. Sbt can easily cross-build over multiple Scala versions, but I don't think it can do it out of the box for multiple versions of a library.


rwijngaa February 2016

Thanks @dth, you put me on the right track. The settings below worked for me:

libraryDependencies ++= {
  Seq(
      javaJdbc,
      javaEbean,
      cache,
      javaWs,
      "org.webjars" %% "webjars-play" % "2.3.0-2",
      "org.webjars" % "bootstrap" % "3.3.6",
      "org.webjars" % "font-awesome" % "4.5.0",
      "be.objectify" %% "deadbolt-java" % "2.3.3",
      "org.apache.lucene" % "lucene-core" % "3.6.0" % "compile,test",
      "org.elasticsearch" % "elasticsearch" % "1.7.4" % "compile,runtime",
      "org.elasticsearch" % "elasticsearch" % "1.7.4"  % "test" exclude("org.apache.lucene", "lucene-core"),          
      "javax.jcr" % "jcr" % "2.0",
      "org.apache.jackrabbit" % "jackrabbit-core" % "2.11.2",
      "org.apache.jackrabbit" % "jackrabbit-jcr2dav" % "2.11.2",
      "org.apache.tika" % "tika-parsers" % "1.11",
      "org.apache.tika" % "tika-core" % "1.11",
      "commons-io" % "commons-io" % "2.4",
      "com.typesafe.akka" %  "akka-testkit_2.11" % "2.3.14" % "test"     
   )
}

Post Status

Asked in February 2016
Viewed 2,736 times
Voted 4
Answered 2 times

Search




Leave an answer