Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean up Maven dependency management

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.

like image 661
anselm Avatar asked Jun 08 '11 15:06

anselm


People also ask

Does Maven clean remove dependencies?

mvn clean do not clean your repository. It only cleans the project.

What does clean in Maven do?

The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.


2 Answers

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
like image 92
Raghuram Avatar answered Oct 07 '22 14:10

Raghuram


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.

like image 33
Dónal Boyle Avatar answered Oct 07 '22 14:10

Dónal Boyle