Using transitive dependencies the right way

So we have this cool feature called transitive dependencies in maven 2. For those that don’t know yet that allows me to say:

  • me: I want struts 1.2.8 in my webapp, do it!
  • maven: sure, I add struts-1.2.8, but as I know you’re a bit lazy I’ll add for you commons-beanutils, commons-digester, jakarta oro,… because I know you’ll need them
  • me: cool, I still remember those days when I had to download the jars by hand and put them in a folder in cvs and then create a classpath in ant,…
  • maven: no problem

But then something happens with some of the jars:

  • me: I want to use spring-hibernate 1.2.5
  • maven: you again? I’ll add spring-hibernate-1.2.5, but as it depends in other stuff, I’ll add also spring-orm and its dependencies, hibernate-annotations, hibernate 2, hibernate 3,…
  • me: wait! how is that you’re adding hibernate 2 and 3? I want to use just 3!!
  • maven: well, if you wanna use all the features in spring-hibernate you’ll need both or you may get a ClassNotFoundException. There’s no way I can know what will you use and what not.
  • me: let me think, if I had created a spring-hibernate2 and spring-hibernate3, I could depend on spring-hibernate3 and everything would be automatic
  • maven: yes, but shit happens, and that’s not how it was released
  • me: ok, then let’s tag hibernate 2 and 3 as optional in the spring-hibernate pom in ibiblio, that way I don’t need to exclude hibernate2, although I’ll need to add also hibernate3 to my dependencies.
  • maven: that will be hard for a lazy bastard like you
  • me: stfu!

To sum up this is my advice: the days of downloading jars and adding them to a folder ended time ago, don’t be worried about creating more smaller jars that explicitly define their dependencies with no doubt, meaning that you’ll always know the requirements of a piece of software. Avoid big jars that depend on everything.

(Thanks to all the people that contributed to this entry teaching me the wonders of the english cosswords. You’re more than you think 😉 )

6 thoughts on “Using transitive dependencies the right way

  1. Carlos,

    Good blog entry.
    I’ve spent the past few days getting the source code for my book ready for release using maven2.
    Its not been fun dealing with transitive dependencies that seem to include the kitchen sink.
    (I’ve discovered all kinds of open source libraries that I never knew existed).
    Small jars and optional dependencies are definitely good thing.

    Chris

  2. [Trackback] There is a funny ( but useful ) tips explaning how to deal with the transitive dependencies system of maven2 when using large frameworks (struts, myfaces, hibernate, spring etc…) on Carlos Sanchez’s Weblog.

    There is a hell lots of questions on the…

  3. The current pom for MyFaces is messed up as well. It marks servlet-api-??.jar and jsp-api-??.jar as runtime instead of optional. The result… when I run war:inplace it copies the jar files to my war’s WEB-INF/lib… this causes no JSPs to load and with no error messages. It took me a while to figure out what happened (when I could not even get a simple hello world jsp to load).

    This is a good post… I’ve been down this road. It is a bit bumpy. It will smooth out overtime. Thanks again.

  4. Carlos,

    That’s a very important feature of Maven 2 indeed. There needs to be more blog entries & articles evangelizing this.

    I’ll soon add one such entry on my blog, about the use of Maven 2 with DWR.

    BTW, you and me have been working together last week on a potential Maven 2 bug, concerning the optional dependencies. Have you managed to understand what was going on?

    Julien Dubois

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s