Tag Archives: mvn dependency plugin

Knowing the dependencies amongst your jars.

It may happen often that we include a bunch of jar files in our project, but not sure about their usage. We do it many a times as we want to avoid any unforseen errors, and most of the times just for saving time’s sake which we have to spend examining each’s utility.

I too did the same, until yesterday when I intentionally started removing the jars whose utility I couldn’t directly see, and there awaited a lot of surreal surprises, and of course a plethora of errors, which I don’t normally see, all because of an inquisitive idea!

This post will again extol maven. There were days when I would just loath this word, for it required me to change my directory structure and write an additional file, POM. But gone are those days, and my ignorance about it.

Let’s take a sample POM.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>ankeet.spring.aop</groupId>
 <artifactId>spring-aop-sample</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>spring-aop-sample</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
<dependency>
 <groupId>cglib</groupId>
 <artifactId>cglib-nodep</artifactId>
 <version>2.2.2</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.6.12</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjrt</artifactId>
 <version>1.6.12</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>
</project>

Now when I save it in STS, the maven plugin downloads these dependencies and adds them to my classpath.

So, no surprise there, four dependency declarations and four dependencies listed above.

Now, add one more dependency to your POM and save it.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.1.0.RELEASE</version>
 </dependency>

We just added a spring-context dependency to our project. Now we’ll see the dependencies included in the project with the inclusion of this new one.

What in the world???

We just included one single dependency!!

From where have all these come from?

It’s okay, don’t panic there’s no error, or virus for that matter. 🙂

By now you must have guessed, these extra jars are the dependencies of spring-context dependency. Spring-context depends on them directly or indirectly, so you have to include all those in your classpath but if you use maven, it’ll do it automatically for you. 🙂

To know it in a more detailed manner, type

mvn dependency:tree

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-aop-sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ spring-aop-sample ---
[INFO] ankeet.spring.aop:spring-aop-sample:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-context:jar:3.1.0.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:3.1.0.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:3.1.0.RELEASE:compile
[INFO] | +- org.springframework:spring-core:jar:3.1.0.RELEASE:compile
[INFO] | | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | +- org.springframework:spring-expression:jar:3.1.0.RELEASE:compile
[INFO] | \- org.springframework:spring-asm:jar:3.1.0.RELEASE:compile
[INFO] +- cglib:cglib-nodep:jar:2.2.2:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.6.12:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.6.12:compile
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.297s
[INFO] Finished at: Tue Dec 20 11:32:02 IST 2011
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

So you can see how spring-context is dependent on spring-aop and spring-aop inturn is dependent on aopalliance.

There are several other useful goals of this maven dependency plugin like analyze. You can check them here.

I hope you’ll too have fun discovering the very jars you unknowingly used and their obscurely hidden interdependencies. 🙂

Advertisements