views:

279

answers:

5

Perhaps the reason I stalled learning Java until now is because I HATE how Java handles external libraries. I'm stuck keeping them in one place, adding them individually, fixing problems with versioning and every time I move/rename them, and copying and writing the classpath over and over each time I release a Java application.

There has to be an elegant solution to all of this. I keep all of my libraries (regardless of task, platform, or other) in their own little folder inside a "lib" folder in my development folder, kind of like this:

Dev
  -lib
    +JS-jQuery
    +Flex-Degrafa
    -Java-Xerces
      +Xerces-1.2.3
    +More libraries

I can use either Netbeans or Eclipse for Java dev, but none of them provide a very streamlined (and not to mention idiot-proof) way of managing all of these.

A nudge in the right direction or an online article/tutorial on this would be greatly appreciated.

+7  A: 

You can either use Ant + Ivy or Maven to manage your library dependencies.

ChssPly76
+1 for maven than can generate IDE specific files (so you get IDE independence) like Eclipse's .classpath and .project.
Pascal Thivent
@Pascal T: Netbeans reads the Maven files directly and uses them for project configuration, rather than translating to an IDE-specific file.
Steven Huwig
That's good but, honestly, generating IDE specific files is not something I do 10 times per day, so it's not a big deal for me. Moreover, I don't really like wizards to manage my pom.xml files and I prefer to do it by hand so I'm not looking after built-in support or plugins. In other words, and that's just my point of view, I think we can live without it.
Pascal Thivent
The maven plugins don't require yuo to use the pom.xml wizard. The built in editors include a "source" tab that lets you edit the xml directly. IAM even lets you set the source tab as the default tab.
Chris Nava
+1  A: 

If you're working on Linux you can install Java libraries with APT or RPM.

Otherwise, I normally check precompiled JARs into a lib directory in my project's version control repository and make sure the names of the JAR files include full version information. E.g. lib/foo-1.5.6.jar, not lib/foo.jar.

To avoid having to manually set the classpath before running your app, you can set the classpath in the Manifests of the JARs themselves to define the dependencies of each JAR file. The JVM will follow all the dependencies when loading classes.

Nat
+1 for keeping the version numbers in the filenames of jars.
Sam
+6  A: 

If it is only dependency management you're after and you're happy with the rest of your build process, I would use Ivy, as it can unobtrusively manage your dependencies, leaving your existing build process intact. There is a plugin for Eclipse called IvyIDE that contributes your dependencies via a classpath container.

Maven 2 has a steeper learning curve but provides a much richer set of functionality for building your projects and Eclipse integration through m2eclipse or IAM.

Personally I use Maven as I have a large number of projects to work with and Maven is particularly suited to efficient development across lots of projects.

Have a look at the introductory documentation to see what works for you.

Rich Seller
+2  A: 

Netbeans 6.7.1's Maven support is quite good and comes out of the box with the IDE.

The Eclipse addon was frustrating enough that I gave Netbeans another try.

A third choice besides ChssPly76's options is to use Ant with the Maven Ant Tasks. I don't know if I'd call any of these solutions particularly "elegant," but they do spare you the need to manage your own lib/ directory and classpath variables.

Steven Huwig
A: 

Maven is often more trouble than it's worth, but the ability to open a maven project directly into IDEs such as IntelliJ is excellent. For example, IntelliJ will download all dependencies and have them available without having to run a build first, or an mvn command and then a project refresh. It also isn't necessary to re-generate the project every time a dependency is added. I work with a number of Eclipse developers who switched to IntelliJ for this alone.

However, one shortfall of Maven is that many libraries (or versions of libraries) are not available on public repositories. Therefore it is often necessary to set up a local repository such as archiva. In ant, it would just be a matter of adding it to the lib directory in the repository.

Maven can also attack when you need to do something that maven doesn't directly support via a plugin. What would normally be a few lines of ant can often turn into a morning's worth of work.

Finally, buildr is an excellent way of using Maven's dependency management and plugins, while also supporting ad-hoc tasks.

rhys keepence
"Subjective point of view on Maven added value" - not subjective at all, it is based on real world experience maintaining maven builds on a number of projects. "bad/irrelevant arguments about IDEs advantages" - Irrelevant? I suggested that using certain types of IDEs do help you if you use maven over ant. Once again this is based on real experience watching people struggle with Maven. "outdated recommendations (go for Nexus, not Archiva)" - Outdated? No, just subjective. Besides, it wasn't a recommendation.
rhys keepence