How to remove duplicate dependencies from pom?
Asked Answered
O

4

5

I have a multi-module project in maven where some of the modules depend on other modules. Now the modules that act as dependencies have some of the dependencies which are already listed in the dependent module's pom.

Is there a quick way to identify such duplicate dependencies and remove them from the dependent module's pom?

Oldham answered 23/1, 2017 at 12:9 Comment(0)
G
6

A project's dependency tree can be expanded to display dependency conflicts. Use command

mvn dependency:tree -Dverbose=true

to identify such duplicate dependencies. It shows all duplicates and conflicts in the pom.

Use the <exclusions> tag under the <dependency> section of the pom to exclude such duplicate dependencies.

<dependencies>
    <dependency>
      <groupId>sample.ProjectA</groupId>
      <artifactId>Project-A</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  <!-- declare the exclusion here -->
          <groupId>sample.ProjectB</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
Gonsalve answered 23/1, 2017 at 12:20 Comment(3)
can you give an example of how the mvn dependency:tree command is useful in this case?Oldham
when you run this command with -Dverbose=true its shows all the duplicates and conflicts.Gonsalve
This is proper solution.Loud
G
3

If you are using eclipse as IDE then the duplicates can be seen in the dependency hierarchy of the concerned pom.xml. And using exclusions tag they can be ommitted.

Guanidine answered 24/1, 2017 at 5:15 Comment(0)
N
2

You can use mvn depgraph:graph -Dincludes=ets.tkt -DshowDuplicates -Dscope:compile. To use this plugin put this on your settings.xml

<settings>
  . . .
  <pluginGroups>
    <pluginGroup>com.github.ferstl</pluginGroup>
  </pluginGroups>

</settings>

When you run the previous console command, you can go to /target and you will find a .dot file. you can render this file using graphviz. More info at https://github.com/ferstl/depgraph-maven-plugin

Nasalize answered 5/12, 2019 at 9:58 Comment(0)
R
0

There is a JBoss tool to help with these issues: http://tattletale.jboss.org/ Unfortunately, it seems that is not under active development these days.

Reo answered 27/1, 2018 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.