Best practices for adding 3rd-party libs to my project

Hi, I'm evaluating IntelliJ IDEA v11.1.4 on my Mac.  I'm trying to add a simple 3rd-party library to my new project, and I'm looking for a best practice suggestion on how to do this.  In my limited experience with Java, I typically end up needing to add a dozen or so libraries to my project, and most all of them come with 3 constituent pieces:  somelibrary-docs.zip, somelibrary-src.zip, and somelibrary.jar.  (the library I'm currently working with, sqlite4java, just happens to have this jnilib business but I'm pretty sure this is not typical so we can ignore this for the sake of simplicty)

To mimic the best practices I've seen used in the past on a certain other IDE, I've set my project up as follows in IntelliJ

MyProject
  .idea
  doc
    sqlite4java-docs.zip
  lib
    libsqlite4java-osx.jnilib <-- let's ignore this for now per above comment
    sqlite4java.jar
  src
    MyProject
    sqlite4java-src.zip
  tests
  MyProject.iml



Now, it seems like there are two ways to make IntelliJ cognizant of a 3rd-party library, its javadocs, and its source.  The first way is by going to Project Settings : Modules : Dependencies, and doing some voodoo magic here, such as designating a directory (lib) as a jar directory - but then, how do I attach javadocs and src to each jar?  A second way apparently is to add the library using Project Settings : Libraries to add the library's jar, and then somehow magically attaching javadocs and source code through a settings panel that is not evident to me.  

Since I'm OCD, even with dozens of libraries, I like explicitly to attach its source and javadocs, so using the first way and adding libraries one-at-a-time works best for me.  Others tend to prefer to dump all the JARs into a directory, and to hell with documentation and source, so designating a directory as a JAR directory and dumping in 3rd party library JARs with impunity is preferred; this would be the second way, as I understand it.

Can someone kindly help articulate the voodoo magic phase of this into actual steps that I can do in this IDE?  Thanks!

7 comments
Comment actions Permalink

Just right-click on your lib directory and select Add as Library. If you have sources and javadocs in the same directory then you are done, IntelliJ will find and configure the sources and javadocs (although if you have the source jars you don't need the javadoc jars since javadocs are in the sources).

If you need to add sources later go to File->Project Structure->Module->Dependencies tab->select your library->then click on the pencil icon near the bottom.

This will bring up a dialog where you can add more jars, sources, or javadocs to your project. Just choose the + sign and then "Add files or directories" (or from a URL). You can add 1 or more jars at the same time by shift-clicking and range or ctrl-clicking individual jars.

The key thing to know is that it doesn't matter what kind of jar it is, you add it to your project the same way and IntelliJ sees what kind it is and acts appropriately.

0
Comment actions Permalink

Hi Michael, thanks for your prompt reply.  It sounds like I was on the right track, but it's good to have confirmation.  

I wasn't sure IntelliJ was working correctly because it wasn't displaying the JavaDocs as I was typing in parameter names for one of the methods in the just-added library, but it turns out this was a user error!  I guess you have to hit Control-J (on mac) to see the JavaDocs pop up, since IntelliJ doesn't show them automatically while you type -- I'm used to this automatic popup behavior from a certain other IDE (I kind of liked this behavior) so I thought something was wrong with how I added the lib to my IntelliJ project!  

Also thanks for the tip about javadocs being redunant if you've added the source for a library.  I never really stopped to think about it, but this makes perfect sense since the javadocs are parsed from the src, and indeed I still get JavaDocs comments with only the somelib.jar and somelib-src.zip files added to my project (AND pressing ctrl-j!).  There is no need to add somelib-docs.zip -- one less file to worry about.

Neat IDE.  I'm having fun learning how to use it.

0
Comment actions Permalink

Have you considered justing Maven to handle your dependencies for you?

0
Comment actions Permalink

I'll be a bit more blunt than Jonathan Logan and go as far as saying that manual dependency management is a colossal waste of time. :) Learn about Maven and Gradle for project management, and Ivy if you just want dependency management.

0
Comment actions Permalink

Even if you use Ivy for dependency management you still have to configure a library in IntelliJ for wherever Ivy downloads the jars too. Unless there has been a recent change to IntelliJ that I am unaware of IntelliJ does not configure itself from a Ivy configuration. So regardless he needs to know how to configure a library in IntelliJ.

IntelliJ can configure itself from a maven project, but all he wants to know is how to configure a library, telling him to switch to a whole new project management tool seems overkill.

0
Comment actions Permalink

"all he wants to know is how to configure a library"

I took it as a question about "best practices for adding 3rd-party libs to my project." Sure, the information about adding a library manually is valuable and useful. But when you're dealing with more than a handful of dependencies and trying to follow the latest stable versions of those dependencies, manual management becomes a maintenance nightmare. I also didn't tell him to switch, I suggested he learn about them to see if they suit his needs.

0
Comment actions Permalink

Thanks, in hindsight after reading this thread, I probably should have asked for "best practices for manually adding 3rd-party libs to my project".  That was the information I was seeking.  Not the philosophy behind library management in a project, which is the topic that into which we have diverged, though of course this is also a very valid point & useful discussion.

To that end, I am not comfortable with having the libraries I've coded against and done extensive QA testing on suddenly automatically update to a new version that is allegedly stable.  For this reason, I prefer coding & testing against a specific version of a library, and sticking with that specific version for the lifecycle of the product release.  I would consider updating to a newer version of the libraries at the beginning of the next minor version of the product, but probably only if it fixed a known issue or added a desired API.  Otherwise, maybe I'm old fashioned, but I don't associate "newer" with "better" in much the same way I don't associate "new version" with "stable".  :)  And stay off my lawn.

0

Please sign in to leave a comment.