2

If I include a library (e.g. mycompany.model.core-SNAPSHOT) within an application (e.g. my.company.app), and mycompany.model.core contains Hibernate as a dependency, can I reference Hibernate classes and functions within my.company.app WITHOUT including hibernate (explicitly) in the app?

Thanks!

Sam Levin
  • 3,326
  • 7
  • 30
  • 44

1 Answers1

3

You can, but it's not considered good practice.

If you need a class or interface to be on the classpath in order to compile your application, you should add a <dependency> for it. This protects you from changes to your upstream dependencies (e.g. if the library that you depend on switches to TopLink instead of Hibernate) and allows Maven to notice if the version numbers of your direct and indirect dependencies get out of sync.

Since you control both the library and the application artifacts, why not make them both inherit from a common parent pom.xml? Then you can manage the versions of your dependencies (e.g., your Hibernate version) from one place.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • Why is it not considered "good practice"? The dependency is still centralized if you use a parent pom which specifies the version, I just didn't want ANY hibernate in my dao later but I guess that is impractical. – Sam Levin Jun 27 '12 at 02:56
  • @SamLevin: in your case, since you control both artifacts, I don't think it's a problem. As for why it's generally best practice, I gave some reasons in my answer. If you need more, then you might want to post a follow-up question to get more responses. – Daniel Pryden Jun 27 '12 at 06:32