20

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.

Vitaliy
  • 8,044
  • 7
  • 38
  • 66
  • Why do you separate these proceses - build and test? – Eugen Martynov Sep 02 '12 at 20:59
  • Are you able to modify the pom.xml of both projects? – noahlz Sep 03 '12 at 00:06
  • Also, can you detail what you have tried so far? You mention that you have "exploded the jar into class files." Did you do this manually, or using the Maven Dependencies Plugin? – noahlz Sep 03 '12 at 00:13
  • @noahz, yes I am. I am trying to follow this link: http://maven.apache.org/guides/mini/guide-attached-tests.html but I am not sure this is the correct direction. – Vitaliy Sep 03 '12 at 05:23
  • As Eugen mentioned, attaching test jars is standard, but executing them in a different project is not. – noahlz Sep 03 '12 at 05:41

3 Answers3

12

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

Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
  • 1
    That's too bad.. It does not seems like such a far fetched requirement though. I will resort to option 2. Thanks. – Vitaliy Sep 03 '12 at 06:28
  • Yes, it is on the roadmap... But just not implemented yet – Stephen Connolly Sep 03 '12 at 07:15
  • 1
    Ahem... I am on the Maven PMC... it's on my list of itches to scratch, as it is also on Kristian's (who does most of the Surefire releases recently) list... the issue is more that it is not a pressing itch, and neither of us have felt the immediate pressing need to scratch that itch over other itches. If you want to prepare a patch I will be more than happy to review said patch with a view to committing it. See: http://javaadventure.blogspot.ie/2012/07/do-you-want-to-become-maven-committer.html for what a patch would need to be like – Stephen Connolly Sep 03 '12 at 08:37
  • Oh! Forgive me for the ignorance. What is a PMC? (I know PM..) – Vitaliy Sep 03 '12 at 08:43
  • In the same context, could you please clarify the purpose of this: http://maven.apache.org/guides/mini/guide-attached-tests.html – Vitaliy Sep 03 '12 at 08:57
  • Change "Many times you may want to reuse the tests that you have created for a project in another" to "Many times you may want to reuse the test classes that you have created for a project in another". You may have abstract classes or helper classes that you want to reuse... and additionally when we pull the finger out and implement the feature you are looking for it will help then – Stephen Connolly Sep 03 '12 at 09:27
  • Maven PMC = Maven Project Management Committee... the people entrusted by the Apache Software Foundation to run the Maven Project. There are additional responsibilities which we have, like voting on releases, etc. All PMC members are developers of Maven but not all developers of Maven are PMC members. Some people want to focus on developing Maven and may not want to be distracted by the PMC duties, so the PMC does not dictate what is done or the plan. IOW we don't dictate what is done, but we have an idea what the devs want to do ;-) – Stephen Connolly Sep 03 '12 at 09:32
7

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)

Community
  • 1
  • 1
mac
  • 2,672
  • 4
  • 31
  • 43
1

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.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Do you mean write a test that programatically executes the tests in a specific jar using JUnitRunner? – Vitaliy Sep 03 '12 at 06:21
  • For example. You need something bootstrap the tests which are in the jar. The usual tests (src/test/java) will be started by surefire... – khmarbaise Sep 03 '12 at 06:45