sbt-assembly include test classes

2020-07-23 04:46发布

问题:

I follow sbt-assembly : including test classes from a config described in https://github.com/sbt/sbt-assembly that work ok doing assembly

When I load sbt I get

assembly.sbt:5: error: reference to jarName is ambiguous;
it is imported twice in the same scope by
import sbtassembly.AssemblyKeys._
and import _root_.sbtassembly.AssemblyPlugin.autoImport._
jarName in (Test, assembly) := s"${name.value}-test-${version.value}.jar"
^

So, I comment import line and run sbt:assembly but that begin the test but dont generate any -test-.jar.

Any one know how to generate the jar that include the test classes? Thanks

回答1:

I had to remove this line (I think it is now autoimported based on https://github.com/sbt/sbt-assembly/blob/546d200477b64e2602beeb65bfa04306122cd9f5/Migration.md)

import sbtassembly.AssemblyKeys._

And I added the rest (i.e. the two lines below) to build.sbt instead of assembly.sbt:

Project.inConfig(Test)(baseAssemblySettings)
jarName in (Test, assembly) := s"${name.value}-test-${version.value}.jar"

After taking those steps, test:assembly does produce a test jar for me however I expected the jar to only include test classes (similar to test:package), but it seems to include non-test classes as well. In other words, if I have src/main/scala/Foo.scala and src/test/scala/FooTest.scala then I thought that the jar produced by test:assembly would only include FooTest.class but it seems to also include Foo.class. Hopefully that's not an issue for you as I'm not yet sure how to workaround that.

EDIT: If you want the jar to only include classes from src/test (like I did), then you can add the following to your build.sbt to filter out everything else that may be on your classpath:

fullClasspath in (Test, assembly) := {
  val cp = (fullClasspath in (Test, assembly)).value
  cp.filter({x => x.data.getPath.contains("test-classes")})
}