views:

119

answers:

3

How can I systematically determine which jars I'll need, and thus should include in my pom.xml file (I'm using maven as my project management tool)?

When learning spring, to keep things simple, added all the jars (even the ones I never used) to the classpath.

Right now for the most part, I'm guessing which jars to include. For example, I know in my spring configuration file, I have:

<tx:annotation-driven />
<context:annotation-config />
<aop:aspectj-autoproxy />

So, I guess I'll need: spring-context-x.x.x.jar, spring-tx-x.x.x.jar, spring-aop-x.x.x.jar

Thanks.

+3  A: 

I see only two possible alternatives. Either:

  • "know what you're doing" and add the required dependencies before the fact ~or~
  • In doubt, don't add the dependency, let things fail and add the dependency after the fact
Pascal Thivent
Not adding the dependency and letting things fail might be worse, since you might not find out about the missing dependency until the worst possible time. For example, say a certain library was used to send mail in case of a critical error - you might never find out about it until that error happened (and even then, maybe not - you wouldn't get the mail!).In short, neither of these suggestions are *systematic*.
Avi
@Avi You may want it but this is not possible in a fully automated way. Tools can help a bit (e.g. `mvn dependency:analyze`) but they won't take runtime dependencies into account. So this leaves you with manual tweak of the classpath and regression testing (assuming you have tests with high coverage). At the end, *"know what you're doing"* is your best weapon, whether you like it or not.
Pascal Thivent
@Avi In the context of *this* question, I'm assuming the OP is using (e.g. during tests) the things he's adding to his Spring context configuration (or I guess he wouldn't add them). Now, if your context differ, feel free to ask a different question. And if you have a better answer to **this** question, feel free to post it.
Pascal Thivent
For the sake of clarity, @Avi removed his initial comment where he was saying that this answer wasn't helpful to find used/unused dependencies accumulated over time (which is not exactly what the OP is asking BTW). My first comment was an answer to his deleted comment.
Pascal Thivent
+2  A: 

For the general problem of finding which JAR(s) contain which classes and their associated dependencies, you can try http://www.jarvana.com/jarvana/. It takes a class name as input and spits out a bunch of Maven dependencies you can use to get said class.

For Spring in particular, I believe you'll have to refer to its documentation. If you have IDE support for Maven, you can typically simply fill in the spring groupId (org.springframework) and activate autocompletion inside <artifactId></artifactId> to see which JARs are available. The main sections in the Spring docs tend to have their own separate jars.

As I'm sure you've seen, another good indicator that you'll need a separate JAR are the XML namespaces used in your applicationContext.xml file. For example, here's an XML root node from a project using aop, tx and beans:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="byName">

The example above would imply that you need the spring-beans, spring-aop and spring-tx JARs (Spring's JAR naming is fairly consistent). I'm not sure if the above is always true, but it should at least give you an indication.

voetsjoeba
+1  A: 

An easy way to completely forget about the JAR files is to use nexus.

If you're a corporation, you can set up your own Nexus server. If not, use a public server such as http://repository.sonatype.org (note: I haven't used this yet as we have our own) and search for the library. Once you found the library, copy/paste the <dependency> ... </dependency> section into your POM file and you're good to go.

I forgot how we linked maven with the nexus server, but it's not too hard.. do a bit of searching and you're golden. No more worrying about JAR files!

Joao