2

If I have the following two dependencies in the same pom.xml file:

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
    </dependency>

And I'd like to remove the redundancy. So should I delete the one with scope as runtime because it's included in the other dependency?

Also I'd be happy to understand why one would specify a dependency with scope of runtime.

Jens Bannmann
  • 4,845
  • 5
  • 49
  • 76
Hanna Khalil
  • 975
  • 1
  • 10
  • 28

1 Answers1

2

From Introduction to the Dependency Mechanism - Dependency Scope:

  • compile
    • This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

(...)

  • runtime
    • This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

So if you have a compile dependency, runtime is already included and therefore superfluous.

As an example for when to use runtime, take the SLF4J logging API: you compile your sources against slf4j-api.jar (compile dependency), but not the actual implementation, which is distributed separately (and there are several to choose from). However, when packaging your application or running unit tests, Maven should still include an implementation jar, e.g. slf4j-simple.jar (runtime dependency), because otherwise nothing will be logged.

Jens Bannmann
  • 4,845
  • 5
  • 49
  • 76