Is it necessary to repeat the dependencies (JAR)?

4

I have a "project A" that uses a .JAR library, which is inside the dependencies. I've exported this project to .JAR for use in "Project B". This project B must also use the same library.

Do I need to use both or just one of them?

    
asked by anonymous 11.01.2016 / 12:15

1 answer

4

Yes, if project B depends directly from the library it should be included in the classpath both during compilation and during execution.

Something that needs to be understood in Java is that having one of your classes compiled into a JAR does not mean that it has its built-in dependencies. There are some tools that can pull dependencies into the JAR, but that's not the rule and it's not recommended in most cases.

Even when project B does not depend directly on the library but makes indirect use of it, you will still need to include it at runtime (command java ), although it is not necessary at compile time (command javac ).

What sometimes confuses us is that tools like Maven and Gradle automatically bring dependency dependencies into a given project, called transitive dependencies. This helps when we do not depend directly on them, but it is not uncommon to use dependencies passed directly, which is an error, since these transitive dependencies can change if the direct dependency is updated version, p budget example, and it is not intuitive for maintenance of the project.

The only case in which you do not need to include the library in project B, either in compilation or execution, is when you do not make direct or indirect use, that is, you only use a subset of project A classes that , in turn, does not make direct use of the library.

    
11.01.2016 / 23:01