maven - transitive dependencies

Roxana picture Roxana · Mar 2, 2013 · Viewed 8.4k times · Source

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>
  <groupId&gtorg.springframework</groupId>
  <artifactId&gtspring-aop</artifactId>
  <packaging&gtjar</packaging>
  <version&gt3.1.1.RELEASE</version>
.....

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

---

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

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.

Answer

Ryan Stewart picture Ryan Stewart · Mar 2, 2013

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.