Solving Cyclic Dependency

hi,

I have the following condition, which creates a cyclic dependency error which I'd very much like to solve with as little code duplication as possible...

Every module creates a test-jar. all the modules consume common's test jar (which contains test base classes, test annotations, etc.)

The parent reactor defines all modules as sons. Also, it defines the dependency for common-jar in it's dependencies, so that all child modules could inherit that dependencies. This propogates down to all child modules, including common. Hence, a cyclic dependency is creates betwen parent -> common -> parent

it looks like this:

Reactor:
   modules: common, svc, web, ...
   dependencies: common-test (scope: test, classifier: tests)

Common:
   parent: Reactor

   dependencies: common-test(inherited), ...

Web, Svc, ...

any ideas welcome!
thanks,
Zvika

Comments

My Opinion is that best practice is to declare common-test in dependency-managment in its parent and redeclare it in every child module that needs it, I think the pom is more readable that way. but its only my opinion.

In the customer where I work someone solved it by writing a dependency plugin that uses ant style 'if' statements in conjuction with a property, like:

if (this module needs common-test)

   then download it

 

but I think this is ugly.

 

It might be too cumbersome, but you can add yet another level in the hierarchy, and push down the dependency from the parent to the new level (I called it SomeNewModule bellow):

Reactor:
   modules: common, svc, web, ...

 

SomeNewModule:
   parent: Reactor

   dependencies: common-test (scope: test, classifier: tests)

 

Common: 

   parent: Reactor

 

All the modules(i.e. svc, web, dao etc):
   parent: SomeNewModule

   dependencies: common-test(inherited), ...

As you said its a cumbersome solution and an example of how we sometimes create complex solutions to solve simple problems.

we all know that maven is a shit-hole, I usually try not to bury more shit under it.

and BTW, what if the common and web module need another common dependency, would you add another level to the hierarchy?

 

The duct tape programmer

Hi guys,

Thank you for your opinions!

Indeed an interesting debate I often find myself struggling with. Should I try the best I can to make thing pretty(ub this case, "mavenized") or should I go with the simplest thing that works? As much as I hate code duplication (Shalom, I cannot inherit the <classifier>, so defining <dependencyManagement>) has little effect), I hate complex structures and declaring 3 generations in Maven has its own little quirks it adds to the build (inheritance between grandfather to grandchildren is messed up, for one) - So I tend to go with Shalom's "duct tape" approach in this case. It's ugly but it works. Aren't we all? :)