user398920
user398920

Reputation: 1431

How can i detect maven 2 jar dependencies?

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.

Upvotes: 2

Views: 580

Answers (5)

Jan
Jan

Reputation: 9697

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 http://www.sonatype.com/books/m2eclipse-book/reference/figs/web/eclipse_pom-editor-depend-tree.png

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

alt text
(source: jetbrains.com)

Upvotes: 1

Pascal Thivent
Pascal Thivent

Reputation: 570615

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

Upvotes: 0

Jatin
Jatin

Reputation: 709

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.

Upvotes: 0

fasseg
fasseg

Reputation: 17761

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

Upvotes: 2

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

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.

Upvotes: 3

Related Questions