Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one specify transitive dependencies of a test-jar for a maven project?

I'm trying to make sure my test-jar published from maven has the right transitive dependencies.

The test-jar doesn't, for instance, generate with a dependency on the non-test-jar.

Similarly, say AAA publishes a test-jar, and BBB contains a test-scoped dependency for AAA's test-jar. In CCC's tests, when I use a class from BBB's test-jar that uses a class from AAA's test-jar, I get a 'class not found' error on the class from AAA's test-jar - i.e., BBB's tests' transitive dependencies aren't properly recorded at all.

Is there any way to make depending on BBB's test jar properly pull in transitive dependencies?

I have example code for all this in https://github.com/nkronenfeld/transitive-test-dependencies

There are two commented-out dependencies in CCC/pom.xml for the BBB's normal jar, and AAA's test-jar, neither of which seem like they should be needed. CCC's test goal won't however, run without either.

like image 290
Nathan Kronenfeld Avatar asked Feb 11 '18 16:02

Nathan Kronenfeld


2 Answers

As khmarbaise said: Test dependencies are not transitive. If A declares a test dependency on B, this means that A needs B to run its tests. Users of A do not need to know about this. If a test-jar needs something to run, it should declare a compile dependency on this.

like image 123
J Fabian Meier Avatar answered Oct 15 '22 07:10

J Fabian Meier


I ran into this same issue. In my case, I had stub classes in project A, stub classes in project B which depended on A's stub classes, and then I wanted to use B's stub classes in project C.

I think the "right" way to attack this problem is to create two additional projects: A-stubs and B-stubs:

  • B-stubs directly depends on A-stubs (compile scope).
  • A includes A-stubs in its test scope.
  • B includes B-stubs in its test scope.
  • C also includes B-stubs in its test scope.

With that in place, the transitive dependencies of B-stubs should be handled just fine. You shouldn't need to build any test JARs at all.

like image 37
Brandon Avatar answered Oct 15 '22 06:10

Brandon