mikera
mikera

Reputation: 106351

Repeating a dependency used transitively?

I have a project X that depends on project Y and Z

Project Y also depends on project Z

If I specify only that X depends on Y in my pom.xml, then everything still works because dependency Z is obtained transitively.

So are there any good reasons to include the dependency Z at all in my pom.xml? Or is it fine to just leave it out?

Upvotes: 0

Views: 69

Answers (2)

manash
manash

Reputation: 7106

I agree with Andrew's answer but I would like to add some points:

  • You may want to look at this discussion. If you use Z in X's code, explicitly adding Z as dependency to X may cause you some issues. For example, if you add Z as a test dependency (because you are using it only for tests), Z will not be included in the main classpath and you'll get some issues since classes used by Y will not be present.

  • There is a new enforcer rule that has been added to the Maven Enforcer plugin. It allows you to better control transitive dependencies. When enabled, all transitive dependencies are banned and you have to explicitly include or exclude them. See here for more details.

Upvotes: 0

Andrew Logvinov
Andrew Logvinov

Reputation: 21831

It depends. If your project is in fact using classes only from Y then there is no reason to explicitly declare dependency on Z since it's Y that depends on Z.

But I've seen projects that actually use some of the classes both from Y and Z but only declare they depend on Y. I consider it to be a bad practice since: a) you're not declaring all of your project dependencies explicitly; b) if you update Y to a newer version you can run into problems because it either may no longer depend on Z or depend on a newer version that might not contain classes necessary for your project.

Upvotes: 3

Related Questions