I understand the difference between runtime and compile-time and how to differentiate between the two, but I just don't see the need to make a distinction between compile-time and runtime dependencies.
What I'm choking on is this: how can a program not depend on something at runtime that it depended on during compilation? If my Java app uses log4j, then it needs the log4j.jar file in order to compile (my code integrating with and invoking member methods from inside log4j) as well as runtime (my code has absolutely no control over what happens once code inside log4j.jar is ran).
I'm reading up on dependency resolution tools such as Ivy and Maven, and these tools clearly make the distinction between these two types of dependencies. I just don't understand the need for it.
Can anyone give a simple, "King's English"-type explanation, preferably with an actual example that even a poor sap like me could understand?
A compile-time dependency is generally required at runtime. In maven, a compile
scoped dependency will be added to the classpath on runtime (e.g. in wars they will be copied to WEB-INF/lib).
It is not, however, strictly required; for instance, we may compile against a certain API, making it a compile-time dependency, but then at runtime include an implementation that also includes the API.
There may be fringe cases where the project requires a certain dependency to compile but then the corresponding code is not actually needed, but these will be rare.
On the other hand, including runtime dependencies that are not needed at compile-time is very common. For instance, if you're writing a Java EE 6 application, you compile against the Java EE 6 API, but at runtime, any Java EE container can be used; it's this container that provides the implementation.
Compile-time dependencies can be avoided by using reflection. For instance, a JDBC driver can be loaded with a Class.forName
and the actual class loaded be configurable through a configuration file.