sbt-assembly include test classes
Asked Answered
D

2

7

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

Displacement answered 15/7, 2015 at 10:6 Comment(0)
A
6

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")})
}
Achromatic answered 20/1, 2016 at 21:6 Comment(1)
In the latest sbt, this works Project.inConfig(Test)(baseAssemblySettings). Of course, I didn't want to skip other classes so didn't try the remaining part.Bespread
K
0

This works for me:

lazy val root = project.settings(
    assembly / fullClasspath := (assembly / fullClasspath).value ++ (Test / fullClasspath).value
)
Kilbride answered 21/6, 2021 at 17:10 Comment(1)
Then how you run the test jar file?Moolah

© 2022 - 2024 — McMap. All rights reserved.