How is it possible not to use a dependency to compile, but to use it at runtime?

Asked

Viewed 30 times

1

Studying build tools like Maven, I saw that it is possible to declare the scope of a dependency only as Runtime, which would make it impossible to compile a code that uses it. As this is possible, do not compile a dependency but use it in Runtime?

1 answer

1


There are dependencies that do not load all their classes into ClassLoader due to the cost of loading, and even because they can have several separate modules in several Jars.

Imagine the following:

  1. Process of generation of documents

    • The library that is used in those documents pdf, excel and word, also in this library, there are interfaces to make the generation of other formats more flexible by third parties
    • For the generation of each document type, there is a series of classes, perhaps even another complete module (another JAR)
    • Your application will not present compilation error in the absence of these extra Jars, since it is the document generation library that uses them
    • At the time of generation of these documents, the library will load the related classes that can provide the desired generation (pdf, excel, word...)

That’s what the scope is for runtime, you know that one day you can use that module for some functionality, but you don’t use that dependency directly on your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.