5

Say there are two dependencies you need: A and B. And at the same time A is already a dependency of B. So do you still want/need to add A along with B as dependencies in your pom?

I believe this may be needed when A and B are external libraries where the version of A needed may be different than the version of A that B is depending on.

But how about when both your module and A and B are modules in the same project? i.e. knowing their versions are all going to be in sync.

user1589188
  • 5,316
  • 17
  • 67
  • 130

1 Answers1

6

If your module uses APIs from B it's best practice to add it explicitly to your pom, even though it's not strictly necessary. If you upgrade A, it could well be that it doesn't use B anymore and then you'll get a build failure without any changes to your module code.

Regarding versions, you should manage those with dependencyManagement in a parent pom. You can then skip the version for the managed dependencies in the child poms. The version in the dependencyManagement overrides the version in transitive dependencies, ensuring you use the same version everywhere.

If all modules are in the same project, they should also share the same project version. Typically, this will be a snapshot version, e.g. 1-SNAPSHOT

Each module will use something like:

<project>
  <artifactId>A</artifactId>
  <version>1-SNAPSHOT</version>

And refer to A and B like this in other modules:

<dependency>
  <groupId>com.yourcompany</groupId>
  <artifactId>A</artifactId>
  <version>${project.version}</version>
</dependency>

To set a non-SNAPSHOT version before you build a release, you can for example use the maven-dependency-plugin's versions:set goal.

gjoranv
  • 4,376
  • 3
  • 21
  • 37
  • Thank you. So you mean even when working with modules within the same project, we still include all the required dependencies knowing some are included in others already? – user1589188 Nov 08 '18 at 22:50
  • Yes, otherwise the build will be vulnerable to changes in other modules' dependencies. As in your example, if A no longer needs B, but your-module still needs both A and B. If there are lots of dependencies, and you find yourself needing the same set over and over, you can create a pure pom module (let's call it 'common-deps') to define that set and let you maintain it in one place. You can then add 'common-deps' as a dependency to the dependent poms instead of the whole set. Here is an example module from our project: https://github.com/vespa-engine/vespa/tree/master/fat-model-dependencies. – gjoranv Nov 08 '18 at 23:35