Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven - transitive dependencies [duplicate]

I am trying to follow best practices when defining data in pom.xml, so I started to look into the Spring source code, and I have seen:


<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion&gt
  <groupId&gtorg.springframework</groupId&gt
  <artifactId&gtspring-aop</artifactId&gt
  <packaging&gtjar</packaging&gt
  <version&gt3.1.1.RELEASE</version&gt
.....

<dependency>
      <groupId&gtorg.springframework</groupId&gt
      <artifactId&gtspring-beans</artifactId&gt
      <version&gt${project.version}</version&gt
      <scope&gtcompile</scope&gt
</dependency&gt

---

<dependency&gt
      <groupId&gtlog4j</groupId&gt
      <artifactId>log4j</artifactId&gt
      <scope&gttest</scope&gt
</dependency&gt
-----

But, spring-beans also has a dependency on log4j.

Can you please tell me, for the best practice methods, on what extent should you rely on transitive dependencies?

I am asking this because my first thought was not to redeclare the log4j dependency, since spring-beans had already declared it.

like image 649
Roxana Avatar asked Mar 02 '13 18:03

Roxana


People also ask

How do I delete duplicate dependencies in maven?

you can use mvn dependency:tree command to find duplicate dependencies into your project. Use the <exclusions> tag into <dependency> tag of the pom to exclude that duplicate dependencies from maven project.

How does maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

Does maven fetch transitive dependencies?

Maven automatically includes required transitive dependencies in our project. We can list all dependencies including transitive dependencies in the project using mvn dependency:tree command.


1 Answers

Declare dependencies that you explicitly rely on, whether it provides classes you directly import and use or it's something that provides a service you directly use, like Log4J. Transitive dependencies should only supply dependencies that are needed at runtime but that you don't use yourself.

like image 84
Ryan Stewart Avatar answered Sep 22 '22 20:09

Ryan Stewart