Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should transative dependencies in maven be explicitly defined?

Tags:

maven

maven-3

A lot of open source projects like spring consist of multiple modules, when adding those modules as dependencies in maven should I explicitly defined every dependency or just let the transitive dependency management do its thing. For example which of the two cases below is a best practice.

case 1: Include the highest level features that I want

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

case 2: Include each part of the framework as an explicit dependency

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>
            .... other dependencies that are transitively resolved by maven
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

Which is the best practice case 1 or case 2 and why?

like image 943
ams Avatar asked Oct 07 '22 06:10

ams


1 Answers

It depends on if you are declaring any other dependencies that use the same dependencies (in this case, Spring). If all you need is the top-level dependency, declare that. If you are using other dependencies that rely on specific versions of a transitive dependency, declare that as an explicit dependency.

Using Spring as an example: If you declare spring-webmvc and then later decide you need another spring package, say spring-security, it may be a good idea to explicitly define any shared dependencies that they both rely on, just so you know which version is being included with your project.

Basically, declare anything that you need a specific version of and let maven take care of the rest until you either need to exclude versions or declare specific versions. This is what maven is built to do, so let it manage the dependencies until it makes a wrong decision.

like image 60
firelore Avatar answered Oct 10 '22 02:10

firelore