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?

dcsordas
  • 232
  • 1
  • 3
  • 9
  • 1
    Why do you need them in the Jar? – NilsH May 04 '13 at 14:44
  • It is intended to be a test suite in a jar. – dcsordas May 04 '13 at 14:50
  • But why? Is it supposed to be included somewhere else? Is it a test "library" somehow? – NilsH May 04 '13 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 04 '13 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 04 '13 at 14:58
  • @dcsordas - as running the test suite will most likely require adding jars to the classpath (junit?) you might as well put them in their own jar. you may also pull off some classpath stunts in MANIFEST.MF that may save you specifying all those jars on the command line – radai May 04 '13 at 14:58
  • @radai In case of a test suite jar, the jar would only include: test classes; classes which support aforementioned test classes; dependencies of the previous two assets. I see no point in separating them. – dcsordas May 04 '13 at 15:01
  • @dcsordas - so including the test classes wont incur any additional dependencies? you dont use junit? testng? – radai May 04 '13 at 15:02
  • @radai Already answered: "dependencies of the previous two assets". To recap: test classes; test support code; dependencies for test classes and test support code. Nothing else would go into the jar (eg no 'code to be tested' etc). – dcsordas May 04 '13 at 15:06
  • If it's a standalone test project, with no other code, then just put it in src/main/java. – NilsH May 04 '13 at 15:07

2 Answers2

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

Community
  • 1
  • 1
radai
  • 23,949
  • 10
  • 71
  • 115
  • You can create an attached artifact with only the test classes, but it's still a weird requirement... – NilsH May 04 '13 at 14:50
  • @NilsH - i agree the requirement is weird, but if he's sure thats what he wants ... – radai May 04 '13 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 04 '13 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 04 '13 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 '13 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.

NilsH
  • 13,705
  • 4
  • 41
  • 59