Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve my own test artifacts in SBT?

Tags:

scala

sbt

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.

like image 304
Stefan Podkowinski Avatar asked Mar 08 '13 09:03

Stefan Podkowinski


1 Answers

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 [email protected]: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.

like image 89
Schleichardt Avatar answered Oct 04 '22 08:10

Schleichardt