Given three project modules X, Y and Z on a Maven based project, and Y depends of Z and X of Y and X should not know that Y depends of Z, being this:

X -> Y

Y -> Z

X -X> Z

What is the best way to hide the dependency between modules X and Z ?

I'm aware that on Maven it's possible to change the scope of the dependency, but I would like to know if there are any other options for it

  • What do you mean by X not knowing about the dependency between Y and Z? Should the fact that Z exists be completely hidden from users/developers of X, or do you just want to avoid having to declare a dependency between X and Z, something else? – Bart van Ingen Schenau Aug 17 '18 at 10:27
  • Users/Developers of X should not know about Z existence @BartvanIngenSchenau – SEISCOOL Aug 17 '18 at 10:30
  • What is the relationship between Y and Z? Is Z just needed to build Y, or does the produced Y.jar also depend on Z.jar to be able to execute? – Bart van Ingen Schenau Aug 17 '18 at 10:37
  • 1
    Then you are probably out of luck. You can't hide the existence of Z, because of the simple fact that if it is missing from the installation, then the application won't work. – Bart van Ingen Schenau Aug 17 '18 at 10:44
  • 1
    @SEISCOOL In this case, you often want to split the API from the implementation. For instance, you can create the module foo-api which contains the interface FooService, but no implementation. Then, you can create the module foo-core, which depends on foo-api and provide the implementation ActualFooService. Then, if you have another project bar which requires to talk to the FooService, you only make it depend on foo-api. In this case, you can't use any foo-core classes from bar because it does not know it exists, it only knows the public API. – Vincent Savard Sep 14 '18 at 11:40

You do not want the programmers of X to use classes of Z. You can achieve this with the Maven dependency plugin:


This allows you to fail the build if somebody uses a class from an artifact that is not declared as dependency in their own POM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.