Is it necessary to repeat dependencies (JAR)?

Asked

Viewed 52 times

4

I have an "A project" that uses a java library . JAR , which is inside the dependencies. I exported this project to . JAR to use in "project B". This project B also needs to use the same library.

I need to use both or only one of them ?

  • 1

    If you created the jar, you edited the build.xml to import all the necessary dependencies from the A project into your jar, you don’t need to. But if you need to take the library jar along with the one from Project A to make it work, then the answer is yes, you need both.

1 answer

4


Yes, if project B depends on 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 its classes compiled in a JAR does not mean that it has its built-in dependencies. There are some tools that can join the dependencies within the JAR, but it is not the rule and 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 not required at build 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 don’t depend directly on them, but it’s not uncommon to use directly transitioned dependencies, which is an error, since these transitive dependencies can change if the direct dependency is updated version, p budget example, in addition it becomes unintuitive for project maintenance.

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

Browser other questions tagged

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