Reputation: 14458
For example, if there are dependencies:
a -> b
a -> c
b -> c
I want to remove the dependency a -> c
, because there is a -> b -> c
.
I know there may be some strong dependencies which should not be reduced, but it's not relevant to this question.
Example:
In a.pom:
<dependencies>
<dependency>b</dependency>
<dependency>c</dependency>
</dependencies>
In b.pom:
<dependencies>
<dependency>c</dependency>
</dependencies>
Expected result:
In a.pom:
<dependencies>
<dependency>b</dependency>
</dependencies>
Upvotes: 8
Views: 8853
Reputation: 12675
Use mvn dependency:analyze
to show you whether there are dependencies in your pom that you don't need (it may also identify some that you have missed, add -DoutputXML=true
to show the missing entries).
Use mvn dependency:tree
to show you the dependencies currently in use by your project and where Maven is finding them. Add -Dverbose=true
to show all the duplicates and conflicts.
If a
directly depends on c
(that is if code in a
mentions classes in c
), then the pom should reflect it. If a
only depends directly on b
, then you can safely remove the c
dependency from a
's pom.xml file. The commands above should enable you to determine what is the appropriate next action.
Edit: Okay, you updated your question. Here is how you do it:
a
, run mvn dependency:tree -Dverbose=true
. This will show you a complete tree of all dependencies considered by Maven for your project.Or are you looking for a way to do it automatically? I do not think there is any automated way unless you write one yourself, because what you are trying to do is a BAD IDEA. You are telling people that their objections are "irrelevant" to your question, but the fact is that your question is like asking "How can I use Maven to make it more difficult to use Maven?"
There is no good reason why you would want to do this. If you think there is a good reason, then you must be trying to do it to produce some result. You should ask for help with the desired result, because your plan is a bad one.
Upvotes: 15
Reputation: 1575
I assume that you want to find the spurious/unnecessary dependencies that are already met because you get them for free from another dependency.
I can imagine you might want to do that in order to cleanup your poms.
However, it's not normally something what you would like to do, since it's a good practice to explicitly state what are your dependencies.
You never know if in future module b
removes c
as dependency and thus breaks a
Upvotes: 14
Reputation: 43651
Like other posters, I'm not exactly sure what you want to achieve. May be exclusions are what you need? You can use exclusions to remove dependencies of your dependencies - if they are not desired for some reasons.
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>logkit</groupId>
<artifactId>logkit</artifactId>
</exclusion>
<exclusion>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
Upvotes: 2