Whats the best way to resolve dependencies between Java projects?
Asked Answered
O

8

8

I think most of you will know, programmers often reuse code from other software. I think, most of the time it is a good idea. But if you use code from another project your program depends on the other project.

I my current case I got three java projects A, B and C. Now A uses B and B uses C. I'm using eclipse IDE and added B to the buildpath of A and C to the buildpath of B. Now there is an compiler error that A can't resolve something from C. So I have to add C to the buildpath of B.

So what is the best way, to resolve the dependencies while keeping your programm as independent as possible from other projects?

I would like to know is in general and in reference to my current situation. Are there better ways to do this? I.e. there are classpath settings in the launch / debug configuration view, but I think they won't help at compile time.

Thanks in advance.

Othilie answered 18/12, 2009 at 16:59 Comment(1)
Not sure why this was down-voted, and without comment at that. It's a valid question, if somewhat to the left of newbie-ish. Bad form.Concordia
S
7

This sounds like part of the problem set fixed by Maven. Using Maven and Eclipse, namely m2eclipse, you can have projects use other projects and all the dependency resolution is handled for you.

Strawser answered 18/12, 2009 at 17:2 Comment(3)
Also, if you use IntelliJ IDEA, the built-in Maven integration is fantastic.Commentary
We use maven for a large project. I love it once it's setup and working correctly, however the documentation is weak in many places and it can have a steep learning curve. Be aware of those issues before embarking down that path.Compline
If you want to learn more on Maven, there is a free ebook from sonatype here: sonatype.com/Support/BooksStateless
P
2

It sounds to me like you're doing what you have to without incorporating a dependency management tool like Ivy or Maven, which provide you the capability of "transitive dependency management". With either of these tools you can just specify that A depends on B and B depends on C and they will automatically know that A is going to need C as well.

The advantages of Maven (this is what I have experience in) also comes into play when it's time to package your projects for deployment since it can easily gather all of those dependencies (all the way down the hierarchy) and place them together into a distribution folder or a fat JAR that contains all of your dependencies. It takes some reading and set-up time to get into a tool like Maven, but it does make the task of managing your dependencies a whole lot easier, especially as they grow.

Pantomimist answered 18/12, 2009 at 17:6 Comment(0)
M
1

We use Maven and it's essential for our projects. It's a good time for you to learn - dependencies on more than 3 projects can be frightening. Maven deals with versions so that if, for whatever reason, you have to depend on Foo.1.2.3 then Maven will ensure you don't get the wrong version.

However it's not trivial. If you use Netbeans it's built in better than Eclipse and may help you learn. (Also projects are fairly switcheable between the two systems).

Maven supports a lot of concept in its POM (pom.xml) file including licence info, contributors, arguments, etc. so you get a lot more than just dependency management. And it supports modularisation of projects.

Don't skip the learning curve - you need to know how it works. But you will also find previous SO questions that will help

Miscreant answered 18/12, 2009 at 17:22 Comment(0)
A
1

Others have mentioned several of the good tools, maven probably being the most common. Ivy is another one that is more targeted at just dependency management. I personally use gradle which has some of the best of all of those features underneath a familiar groovy wrapper... that is still evolving and spottily documented. ;)

One thing to be aware of is how these tools handle transitive dependencies. In your example, C is a transitive dependency of A because A depends on B which depends on C. Some of these build tools will handle this type of dependency differently and it can surprise you when you least expect it.

For example, if A actually refers to code from C, ie: it has a compile-time dependency on C, then your A->B->C setup will work in something like Maven. On the other end, gradle will also make you declare that A depends on C... since it does. Runtime dependencies are fully resolved either way.

The surprise comes when you've been transitively including something for months and some of your code has relied on aspects of C and you decide you no longer need a B dependency. Suddenly your code won't build until you figure out you need a A->C dependency specified. In this example, that's pretty trivial to discover but sometimes it isn't.

And if talk like that makes your head swim a little and you don't plan on your project getting much more complicated... then you can probably just stick with what you are doing for a while. As others mentioned, it's the right way to do it without a tool helping you.

Alumnus answered 18/12, 2009 at 18:3 Comment(0)
P
1

Use maven to manage your dependencies and then use the dependency plugin to see the dependencies.

you can run

mvn dependency:analyze

or

mvn dependency:tree -Dverbose=true

this will help you a lot.

Pronominal answered 18/12, 2009 at 18:8 Comment(0)
C
1

no doubt you should use a dependency management tool as people have noted... manually though, archive B and C in B_C.jar. Test that B's dependence on C is resolved within the Jar.

Then add B_C.jar in the classpath...

Caressive answered 18/12, 2009 at 18:19 Comment(0)
B
0

Dependency management is a huge topic. Maven, Ivy and other tools have been developed to ease the pain with some success. Both of those tools create a dependency hierarchy so you don't run into the situation you described. They also have Eclipse plugins so that Eclipse will recognize that hierarchy.

To truly use these frameworks, you will have to change your current build process. Maven probably requires more of a commitment than Ivy, but neither is trivial, and understanding how to set it up takes some time. That said, it is very helpful to have your dependencies defined and managed clearly.

Boatman answered 18/12, 2009 at 17:7 Comment(0)
C
0

Free maven books:

http://www.sonatype.com/documentation/books

Cutler answered 22/12, 2009 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.