11

In a big Maven 2 project it is nice to have the dependency management to make sure that only one version of a dependency is used in the whole system. That makes the system consistent.

But when I generate effective POMs I have no chance to see where the dependency versions came from. Likewise in a POM at the top of the hierarchy I have no idea where in the child POMs the defined versions of the dependency management section are really used.

So how do I keep the dependency management cleaned up? When I remove a dependency in one project, I always check in all other projects if it is still needed at all, so that I can also remove in from the dependency management at the top?

Also, how do I build up the dependency management, making sure it is not duplicated somewhere in the child POMs? When I add dependencies I always check all other projects to see if it possibly could be aggregated on top in the dependency management? Or would you just always move all dependency versions to the top from the beginning so they are always in only one place?

Thanks for any thoughts.

1
  • Another idea I just had: why not use the same version number through all modules, and rely on ${project.version} as often as possible. Ideally I only have on version number in the top POM and use the variable everywhere else. Like that I need no dependency management at all, all internal dependency versions are specified directly with the variable.
    – anselm
    Jun 9, 2011 at 12:40

4 Answers 4

9

You could create one or more boms (bill of materials) for your project. These pom.xmls will declare all the dependencies used in your project within dependencyManagement section.

In each child pom, you would import these boms and use those dependencies that are required for the project.

In this way, dependency versions are managed centrally, while at the same time, each child pom uses only those dependencies that it needs.

See Importing Managed Dependencies

BOM project

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group</groupId>
  <artifactId>My-Project-Bom</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.7.0</version>
      </dependency>
      ...
    </dependencies>
  </dependencyManagement>
</project>

Child project

<project>  
 <modelVersion>4.0.0</modelVersion>
 <groupId>my.group</groupId>
 <artifactId>child1</artifactId>
 <packaging>jar</packaging>
 <name>Child1</name>
 <version>1.0</version>
 <dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>my.group</groupId>
       <artifactId>My-Project-BOM</artifactId>
       <version>1.0</version>
       <type>pom</type>
       <scope>import</scope>
     </dependency>
  </dependencies>
 </dependencyManagement>
 <dependencies>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
   </dependency>
 </dependencies>
 ...
</project>

maven dependency plugin has a few goals to help you get the dependency hierarchy.

mvn dependency:list
mvn dependency:tree
mvn dependency:analyze
4
  • Nice idea I did not know about. But since we have one top-level POM that all other POMs are children of, I could just use the dependency management there. I still do not see how this would solve the problem of cleaning up dependency management. The problem inside a BOM would be the same.
    – anselm
    Jun 9, 2011 at 12:28
  • Never knew about this approach. I like it since it gives you the control over versioning but dependencies are still declared where they are required with appropriate scope. Also easy to use a specific version if required.
    – Robin
    Jun 9, 2011 at 14:50
  • Let's go a little bit further: I want a POM with some Spring components and a particular version. Is there a way to do this dinamically? i.e. in child project, import bom project with a version defined in child project. I had a similar question here stackoverflow.com/questions/19156474/… Jan 17, 2014 at 8:57
2

If you are using eclipse, the m2eclipe plugin allows you to view the Dependency Hierarchy for you pom. This can be very useful when trying to determine where dependencies are brought into your project and where conflicts are occurring.

1

You should explicitly declare the dependencies in the projects in which they are used, unless it is being used in ALL of the projects. If, for example, Spring is used for all of your projects, then put that in the parent POM. If it is only used in some projects, declare it in each one and put a spring.version property in the parent which each child pom can use for its version.

Moving all dependencies to the parent removes the responsibility from each project to manage its own dependencies. I would consider this a misuse of maven as it makes things more difficult to maintain instead of easier. It now adds dependencies to projects that doesn't need them. Often the scope of a dependency is different for projects as well, and you cannot manage that unless you declare your dependencies locally.

2
  • I do not see the difference between specifying the version in the dependency management or in a property. Not moving the dependencies to the parent dependency management has the disadvantage to spread version number all over your POMs, easily resulting in having multiple versions of one dependency in the whole application.
    – anselm
    Jun 9, 2011 at 12:34
  • That is why you use a property. Then your dependencies are managed local to the project, but the property defines the accepted version to use. The only caveat is that anyone adding to a child pom has to be aware of the existence of those properties and use them appropriately. This is still much preferable to your proposal which is to basically give every project all dependencies the dependencies of every other project. It also pretty much forces you to scope them all as compile.
    – Robin
    Jun 9, 2011 at 14:45
0

You can get the POM to POM dependencies, and the code-references that cause them, using the Structure101 composition perspective. Create a new s101 project, type Maven, specify the root pom.xml file, finish (use defaults for the rest of the wizard), then select the composition perspective (2nd button down on the vertical toolbar top left of the UI) and you will see something like this:

Structure101 POM dependency graph

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.