Vitaliy
Vitaliy

Reputation: 8206

How to configure pom to run tests packaged in a jar?

I have a maven build process that publishes executable jars and their tests to Nexus. I have another maven build process that needs to access these jars (executable + test) and run the tests.

How do I go about it? So far I have managed to do this only if the jar is exploded to class files. I am new to maven and completely lost in the documentation.

Upvotes: 21

Views: 21161

Answers (3)

Stephen Connolly
Stephen Connolly

Reputation: 14096

Update 2022-03-11

The feature has been implemented, see https://stackoverflow.com/a/17061755/1589700 for details

Original answer

Surefire and failsafe do not currently support running tests from within a jar.

This is largely a case of not being able to identify the tests.

There are two ways to get the tests to run.

  1. Use a test suite that lists all the tests from the test-jar. Because the test suite will be in src/test/java (more correctly will be compiled into target/test-classes) that will be picked up and all the tests in the suite will be run by Surefire/failsafe (assuming the suite class name matches the includes rule: starts or ends with Test)

  2. Use the maven dependency plugin's unpack-dependencies goal to unpack the test-jar into target/test-classes (this screams of hack, but works quite well)

The main issue with the first option is that you cannot easily run just one test from the suite, and you need to name every test from the test-jar

For that reason I tend to favour option 2... There is the added benefit that option 2 does not mean writing code to work around a limitation in a build tool plugin... The less you lock yourself into a specific build tool, the better IMHO

Upvotes: 13

mac
mac

Reputation: 2734

This actually works quite fine with the newer surefire and failsafe plugins, see related questions:

So you don't need to unpack the jar anymore, you just provide the group and artifact id for the dependencies to scan (this works with both "main jar" dependencies, as well as "test-jar" dependencies)

Upvotes: 7

khmarbaise
khmarbaise

Reputation: 97399

The attached test-jar can be used as a usual dependency in other project which supports reuse of code in the test area but you can't run tests out of the jar. If you really need the solution you have to write at least a single suite (etc.?) to start the tests from the jar.

Upvotes: 1

Related Questions