5

I'm struggling with including my test classes into the jar package, but not running them. After some googling, I've tried mvn package -DskipTests, but my test classes are simply not added to the jar.

Any ideas?

10
  • 1
    Why do you need them in the Jar?
    – NilsH
    May 4, 2013 at 14:44
  • It is intended to be a test suite in a jar.
    – dcsordas
    May 4, 2013 at 14:50
  • But why? Is it supposed to be included somewhere else? Is it a test "library" somehow?
    – NilsH
    May 4, 2013 at 14:51
  • @NilsH No, it is not supposed to be reused, but to be a compact solution which can be transported easily (hence why I need a single jar that can be executed promptly anywhere using command line). Is that so weird? I mean, it comes so easily: create a test suite, put it in a jar, move around, execute on demand...
    – dcsordas
    May 4, 2013 at 14:54
  • Yes, it's a pretty unusual requirement. Usually, if you need to distribute the tests, you check out the source code from version control and run the tests. Or distribute a src jar with everything in it.
    – NilsH
    May 4, 2013 at 14:58

2 Answers 2

7

if youre following maven conventions then your test classes are under src/test/java. maven never packages the contents of the test sources subdirectory into the artifact.

you have (at least ...) 3 alternativs:

put the tests with the "normal" sources

if you REALLY want them packaged (why?) then you should place the test classes under src/main/java where they will be treated as normal source and their compiled classes packaged into the artifact (usually *.jar)

you imght run into all sorts of issues doing this. for example your artifact will have a compile-scoped dependency on junit, which might interfere with other modules using your jar.

also, you might have to configure the maven plugins running the tests to be aware of your test classes if you do this (that is if you ever do want to run tests as part of your build). for example the surefire and failsafe plugins.

configure the maven jar plugin to build a tests jar

see the maven jar plugin documentation. they even have a section titled "How to create a jar containing test classes" - the instructions there will cause your tests to be packaged into a separate jar file (which is probably a much better idea).

create the jar your way using the assembly plugin directly

yuo could disable the default execution of the jar plugin and instead add an execution of the assembly plugin to the package phase to create a jar. you will need to write an assembly descriptor for this (not as complicated as the above link makes it out to be). this will give yu full control over what get included in the jar produced. its best to start by copying one of the predefined assemblies

5
  • You can create an attached artifact with only the test classes, but it's still a weird requirement...
    – NilsH
    May 4, 2013 at 14:50
  • @NilsH - i agree the requirement is weird, but if he's sure thats what he wants ...
    – radai
    May 4, 2013 at 14:53
  • @radai Your last edit about the archive plugin seems intriguing, but it totally lacks the information about getting the compiled test cases into the same jar. Could you please elaborate on that?
    – dcsordas
    May 4, 2013 at 15:14
  • 1
    @dcsordas - its a bit more complicated than i remembered - you cant do it with archive configuration but you can do it with the assembly plugin (which is the basis to the jar plugin anyway). i've updated my answer.
    – radai
    May 4, 2013 at 15:26
  • Eventually I went with the jar + test-jar combo (for the time being :P ). Thanks for all the input @radai and @NilsH!
    – dcsordas
    May 22, 2013 at 15:11
3

If it is tests for the current project, then it should be in src/test/java and it will not be included in the main jar. This is the most common use case. If you are writing a test library that will be used in other projects, put them in src/main/java, and add it as a dependency with test scope in those projects.

If you want to distribute everything in a "self contained package", you can either share the code through a version control repository, or create a source jar with the maven assembly plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.