views:

55

answers:

5

I added some jars as dependencies in pom.xml, but it seems that some of them are useless because those jars were already downloaded using dependencies mechanism...

Is there a way to see those "built-in" dependencies, so that I could add only the needed dependencies in my pom.xml?

For example if I add a hibernate dependency in pom.xml one for cglib is not needed.

+3  A: 

Don't do that - list every dependency of your code, but not of the libraries you use; Maven will do its transitive dependency thing and take care of them.

Tassos Bassoukos
But conversely, if your code directly depends on some library, then relying on transitive dependencies for that library (via some other dependency) potentially makes your dependencies fragile.
Stephen C
@Stephen C: Exactly, that's why I dislike maven.
Tassos Bassoukos
You can control transitive dependencies as finely as you want. This is a non issue.
Pascal Thivent
The fragility only comes into play if you depend on snapshots or if you change your dependencies. And it is generally easy to fix. So yes this is mostly a "style" issue.
Stephen C
@Tassos the task of managing transitive dependencies won't go away by not using maven. With maven at least you avoid having jars with unknown version that you don't know if you still need or not.
rlovtang
+2  A: 

you can run mvn dependency:tree to get the whole tree including the transient dependencies that get included in your project. There you can start looking

Hope that helped

smeg4brains
A: 

If you are using Eclipse then you are see a visual dependency tree which is automatically generated. While adding a dependency if you do not want it to automatically pull some transitive dependencies use the exclusions tag like so

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>${hibernate.version}</version>
  <exclusions>
    <exclusion>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
    </exclusion>
    <exclusion>
      <groupId>asm</groupId>
      <artifactId>asm-attrs</artifactId>
    </exclusion>
    <exclusion>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
    </exclusion>
  </exclusions>
</dependency>

There are some known conflicts when using certain versions of hibernate and AOP due to cglib.

Jatin
+1  A: 

First, check out Transitive Dependencies:

Transitive dependencies are a new feature in Maven 2.0. This allows you to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically.

Then, a good dependency analyzer will help...

The mvn command-line is your first aid:

mvn dependency:tree

Sometimes you have to figure out where the version numbers came from (See also: Dependency Management). Here you'll have to unveil the parent relationships, and the 'effective-pom' command can help with that:

mvn help:effective-pom

Tool support is helpful as well...

m2eclipse has a Dependency Tree tab that shows how the different hierarchies collapse::

alt text

IntelliJ has another interesting view that lets you detect the conflicts:

alt text

Jan
A: 

Is there a way to see those "built-in" dependencies, so that I could add only the needed dependencies in my pom.xml?

There are no built-in dependencies. However, when declaring a dependency on a given artifact, Maven will also retrieve the dependencies of this dependency, transitively. Such dependencies are called 3.4.4. Transitive Dependencies:

A transitive dependency is a dependency of a dependency. If project-a depends on project-b, which in turn depends on project-c, then project-c is considered a transitive dependency of project-a. If project-c depended on project-d, then project-d would also be considered a transitive dependency of project-a.

So if you need a dependency in your project, just declare it (and the dependencies of this dependency will come transitively).

To visualize the dependency tree of a project, the best tool is mvn dependency:tree (or any fronted offered by your favorite IDE). This is a must use tool to analyze your dependencies and check them for proper convergence and potential conflicts resulting in expected version being used.

For example if I add a hibernate dependency in pom.xml one for cglib is not needed.

Actually, this is a bad example, cglib is an optional dependency of Hibernate Core which declares in its pom.xml:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-cglib-repack</artifactId>
  <version>2.1_3</version>
  <optional>true</optional><!-- think of it as "excluded by default" -->
</dependency>

Hibernate gives you the choice between javassist and cglib, it's up to you to decide which one to use and to declare it explicitly, hence the optional status.

See also

Pascal Thivent