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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With