Can a program depend on a library during compilation but not runtime?

前端 未结 10 1744
深忆病人
深忆病人 2020-11-30 18:18

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-t

10条回答
  •  北海茫月
    2020-11-30 18:38

    The runtime scope is there to prevent programmers from adding direct dependencies to implementation libraries in the code instead of using abstractions or façades.

    In other words, it enforces to use interfaces.

    Concrete examples:

    1) Your team is using SLF4J over Log4j. You want your programmers to use the SLF4J API, not the Log4j one. Log4j is to be used by SLF4J internally only. Solution:

    • Define SLF4J as a regular compile-time dependency
    • Define log4j-core and log4j-api as runtime dependecies.

    2) Your application is accessing MySQL using JDBC. You want your programmers to code against the standard JDBC abstraction, not directly against the MySQL driver implementation.

    • Define mysql-connector-java (MySQL JDBC driver) as a runtime dependency.

    Runtime dependencies are hidden during compilation (throwing compile-time errors if your code has a "direct" dependency on them) but are included during execution time and when creating deployable artifacts (WAR files, SHADED jar files, etc.).

提交回复
热议问题