11

One of my projects will provide a jar package supposed to be used for unit testing in several other projects. So far I managed to make sbt produce a objects-commons_2.10-0.1-SNAPSHOT-test.jar and have it published in my repository.

However, I can't find a way to tell sbt to use that artifact with the testing scope in other projects.

Adding the following dependencies in my build.scala will not get the test artifact loaded.

"com.company" %% "objects-commons" % "0.1-SNAPSHOT",
"com.company" %% "objects-commons" % "0.1-SNAPSHOT-test" % "test",

What I need is to use the default .jar file as compile and runtime dependency and the -test.jar as dependency in my test scope. But somehow sbt never tries to resolve the test jar.

Stefan Podkowinski
  • 5,206
  • 1
  • 20
  • 25

3 Answers3

19

How to use test artifacts

To enable publishing the test artifact when the main artifact is published you need to add to your build.sbt of the library:

publishArtifact in (Test, packageBin) := true

Publish your artifact. There should be at least two JARs: objects-commons_2.10.jar and objects-commons_2.10-test.jar.

To use the library at runtime and the test library at test scope add the following lines to build.sbt of the main application:

libraryDependencies ++= Seq("com.company" % "objects-commons_2.10" % "0.1-SNAPSHOT"
    , "com.company" % "objects-commons_2.10" % "0.1-SNAPSHOT" % "test" classifier "tests" //for SBT 12: classifier test (not tests with s)
)

The first entry loads the the runtime libraries and the second entry forces that the "tests" artifact is only available in the test scope.

I created an example project:

git clone git@github.com:schleichardt/stackoverflow-answers.git --branch so15290881-how-do-i-resolve-my-own-test-artifacts-in-sbt

Or you can view the example directly in github.

Schleichardt
  • 7,502
  • 1
  • 27
  • 37
  • 1
    This approach may fail to resolve transitive dependencies of the test jar. So it is better to use the [officially recommended way from the documentation](http://stackoverflow.com/a/8194192/495796), after adding the `publishArtifact` line. If it still doesn't work, switching from Maven-style to Ivy-style publishing (with Ivy-style repositories) should work. – Robin Green Aug 05 '15 at 15:37
1

Your problem is that sbt thinks that your two jars are the same artifact, but with different versions. It takes the "latest", which is 0.1-SNAPSHOT, and ignores the 0.1-SNAPSHOT-test. This is the same behaviour as you would see if, for instance you have 0.1-SNAPSHOT and 0.2-SNAPSHOT.

I don't know what is in these two jars, but if you want them both to be on the classpath, which is what you seem to want to do, then you'll need to change the name of the test artifact to objects-commons-test, as Kazuhiro suggested. It seems that this should be easy enough for you, since you're already putting it in the repo yourself.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
-2

It will work fine if you change the name like this.

"com.company" %% "objects-commons" % "0.1-SNAPSHOT",
"com.company" %% "objects-commons-test" % "0.1-SNAPSHOT" % "test",
Kazuhiro Sera
  • 1,822
  • 12
  • 15
  • Doesn't work for me. The published artifact will be stored at /com/company/objects-commons_2.10/0.1-SNAPSHOT/objects-commons_2.10‌​-0.1-SNAPSHOT-test.jar but with your suggestion its expecting to find the jar at /com/company/objects-commons-test_2.10/0.1-SNAPSHOT/objects-commons-test_2.10-0.1-SNAPSHOT.pom which doesnt exist. – Stefan Podkowinski Mar 08 '13 at 11:14
  • I meant that you should change the name from object-commons to object-commons-test if possible. Aren't these libraries under control of your team?? If so, that's too bad... – Kazuhiro Sera Mar 08 '13 at 11:23