137

Possible Duplicate:
Understanding Compile- vs Run-time Dependencies

I understand that a dependency with the "runtime" scope will be available at runtime and not at compile time. But I don't understand why you could want that! Why not simply use the "compile" scope instead?

The docs don't really help. Any idea?

Community
  • 1
  • 1
electrotype
  • 8,342
  • 11
  • 59
  • 96
  • 4
    Check this out: http://stackoverflow.com/questions/7070570/understanding-compile-vs-run-time-dependencies – Wes Sep 04 '12 at 23:13
  • 2
    Thanks Wes, that question is indeed really related. Sadly, I still don't understand! I would use "provided" for dependencies like servlet-api, not "runtime". I'd like to see a real-life example where using "runtime" is better than "compile" and "provided", if possible! – electrotype Sep 04 '12 at 23:29
  • 2
    JDBC Driver mentioned by John Stauffer's answer is a good example. Another example can be SLF4J. You need only slf4j-api for compilation and you SHOULD avoid putting slf4j-log4j12 (or other kind of binding) as compile scope dependency. slf4j-log4j12 is a good candidate for runtime dependency. – Adrian Shum Sep 05 '12 at 03:42
  • Why? Is it because slf4j-log4j12 should be in the app server's classpath? I already know the powers that be will veto this. – user447607 Aug 29 '13 at 17:55
  • 2
    @user447607: No - this would (in theory) allow you to switch `slf4j` bindings without having to recompile the application. – Priidu Neemre Aug 28 '15 at 15:00
  • this question is not duplicated. – Amir Jan 05 '22 at 23:37

1 Answers1

176

runtime is useful for dependencies required for unit tests and at runtime, but not at compile time. This may typically be dynamically loaded code, such as JDBC drivers, which are not directly referenced in the program code.

Setting dependency to provided ensures that there isn't an accidental dependency on the code, and also keeps the dependency from being transitive. So that, for example, if module A has a provided dependency on library X, and module B depends on module A, it does not inherit the dependency on library X. Using "runtime" or "compile" would cause B to depend on X.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
John Stauffer
  • 16,150
  • 10
  • 40
  • 35
  • 11
    But this link: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html Says that provide is not transitive. Is that wrong? – TigerBear Aug 26 '15 at 19:16
  • 8
    Ad transitive dependencies, I have an example of the contrary. After changing a dependency's scope from `runtime` to `provided`, its transitive dependencies are no longer part of my build. – Tobb Oct 21 '15 at 13:56
  • 5
    Runtime *IS* transitive, even though that seems counter-intuitive. I really can't see any difference between compile and runtime, as far as how maven behaves. – Brent212 Aug 02 '17 at 01:24
  • 4
    I don't agree with the very last sentence, as stated here : https://stackoverflow.com/a/6647178/704246. Using "compile" would cause B to depend on X. Using "provided" would cause B to have to explicitly redeclare dependency to X. – Saad Benbouzid Jan 11 '18 at 09:03
  • Contrary to what's mentioned at the end of the answer, the "provided" scope is NOT transitive. – q3769 Oct 23 '22 at 02:36