Maven: How do you feel about dependencies that are both direct and transitive?

I am trying to determine the approach to the following situation:

There are 3 Maven artifacts: A, B, and C.

B depends on A. (i.e. uses some code A)

C depends on both A and B (i.e., it uses some code A and code B).

Suppose I want to use the same version of A for B and C.

Which approach should be used?

1) Declare A as a dependency in C pom.xml.

Pro: it’s clear to the developer that C depends on A. Con: If version A changes, it needs to be updated in several places. (both B and C)

2) Do not declare A as a dependency in C pom.xml.

Pro / Con: opposite option 1.

+4
source share
2 answers

1) Declare A as a dependency in C pom.xml.

Dependency is readable

Dependence is flexible. If you want to remove the dependency on B, you do not need to think about projects that depend on B.

As suggested in another answer , it is good practice to write direct dependencies in pom.xml and let maven handle it.

2) Do not declare A as a dependency in C pom.xml.

Basically, no developer will see pom.xml. And if they want them to be able to see it using mvn dependency:tree , and it will show a transitive dependency.

A single point of change will be released when a new version of A is released. If you define a dependency in more than one place, you may forget to update all places. In this case, Maven automatically uses the latest version. But sometimes it causes a sting.

Some people prefer this because, basically, this type of dependency is well known (e.g. MyWebApp -> MyWebAppLib -> MySharedLib and MyWebApp -> MySharedLib ) and they want to avoid adding a version upgrade step in several places in each version.

I wrote the pros and cons, you have to evaluate what suits you best.


Edit # 1: tsk! I switched my comments.
Edit # 2: update the answer after discussion on this answer .

+3
source

I think you should have all the direct dependencies stated in your pom. Transit dependencies are just a convenience for automatically resolving dependency dependencies.

If you change the direct dependency version, the transitive dependencies are likely to change with it and, thus, can break the module. The module should be built as an independent unit and, therefore, should have well-defined dependencies that will not be interrupted due to external changes.

I do not agree that this violates the DRY principle, since maven defines things within the framework of one project and its pom. And in this area there is no repetition.

Update: Reliance on existing transitive dependencies makes the project weak, and can also lead to more complex problems, for example, when to include it.

For example, if C has a compilation dependency on A, but depends on the runtime on B, then you need to either add a dependency (since it is no longer in your build path), or declare B as a compilation, even if it is not. For clarity, much can be said. Explicitly define what your addictions are and what their scope is, and expect your addictions to do the same. For the most part, your addiction is a black box until it causes a problem and you have to open it.

+4
source

Source: https://habr.com/ru/post/1335152/


All Articles