Caesar Ralf
Caesar Ralf

Reputation: 2234

Tool for finding common dependencies between sibling modules in Maven?

I have to create a new maven project old projects migrated to maven. So I got a structure like this

parent
|
\-- project 1
|
\-- project 2

project 1 and project 2 have tons of dependencies and lots of them are common to each other. What I wonder, and I couldn't find in the internet, is if there is a tool that I can find these common dependencies so I can migrate them to the parent pom?

For examplo, if I provide to this tool two poms with elements like

... PROJECT 1 POM
<dependencies>
   <dependency>
       <groupId>com.foo</groupId>
       <artifcatId>A</artifactId>
       <version>1.0.0</artifactId>
   </dependency>
   <dependency>
       <groupId>com.foo</groupId>
       <artifcatId>B</artifactId>
       <version>1.0.0</artifactId>
   </dependency>
</dependencies>
...

.. PROJECT 2 POM
<dependencies>
   <dependency>
       <groupId>com.foo</groupId>
       <artifcatId>B</artifactId>
       <version>1.0.0</artifactId>
   </dependency>
   <dependency>
       <groupId>com.foo</groupId>
       <artifcatId>C</artifactId>
       <version>1.0.0</artifactId>
   </dependency>
</dependencies>
...

I want the output to be

.. OUTPUT FROM COMPARING BOTH
<dependencies>
   <dependency>
       <groupId>com.foo</groupId>
       <artifcatId>B</artifactId>
       <version>1.0.0</artifactId>
   </dependency>
</dependencies>
...

Upvotes: 13

Views: 2077

Answers (2)

FrVaBe
FrVaBe

Reputation: 49361

I don't know a tool that works like you described but there is a simple workaround:

Make a temporary third project and copy all dependencies from A and B to this pom. Than try to find duplicates with the dependency:analyze-duplicate dependency-plugin goal like this:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:analyze-duplicate

You will get something like this

[INFO] ------------------------------------------------------------------------
[INFO] Building foobar 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze-duplicate (default-cli) @ foobar ---
[INFO] List of duplicate dependencies defined in <dependencies/> in your pom.xml:
        o junit:junit:jar

To make sure that the duplicate really comes from both projects you should duplicate check the single projects on there own before.

Upvotes: 5

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

i don't know any tool to do it..but you can do it at old school

1.resolving all the dependencies : mvn dependency:resolve

2.List all the depencies sorted and with out repeting..and check module by module:

mvn -o dependency:list | grep ":.*:.*:.*" | cut -d] -f2- | sed 's/:[a-z]*$//g' | sort -u

3.then you can look for in all the modules

mvn dependency:tree -Dverbose -Dincludes=commons-collections --> for example

Upvotes: 2

Related Questions