Maven: How to remove dependencies if they are already transitive?

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 that there may be some strong dependencies that should not be reduced, but this is not relevant to this issue.

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> 
+7
maven-2 dependency-management dependencies
source share
3 answers

I assume that you want to find false / unnecessary dependencies that have already been encountered, because you get them for free from another dependency.

I can imagine that you might want to do this to cleanse your pores.

However, this is usually not what you would like to do, as it is good practice to explicitly indicate what your dependencies are.

You never know if module b remove c as a dependency in the future, and thus breaks a

+12
source share

Use mvn dependency:analyze to show if there are any dependencies in your pump that you don’t need (this can also identify the missing ones, add -DoutputXML=true to show the missing entries).

Use mvn dependency:tree to show the dependencies that your project is currently using and where Maven finds them. Add -Dverbose=true to show all duplicates and conflicts.

If a directly depends on c (that is, if the code in a mentions classes in c ), then pom should reflect it. If a depends directly on b , you can safely remove the dependency c from a pom.xml. The above commands should allow you to determine what is the appropriate next step.

Edit: Ok, you updated your question. Here's how you do it:

  • in project a , run mvn dependency:tree -Dverbose=true . This will show you a complete tree of all the dependencies that Maven reviewed for your project.
  • Look at the result of step 1 and make a list of all the dependencies that are shown at more than one level (some of them are likely to duplicate).
  • Edit the pom.xml file in any editor you like and delete any dependencies that match the list you created in step 2.

Or are you looking for a way to do this automatically? I do not think that there is any automated way if you do not write it yourself, because you are trying to do this BAD IDEA . You tell people that their objections are “not relevant to your question,” but the fact is that your question is: “How can I use Maven to make it more difficult to use Maven?”

There are no good reasons why you would like to do this. If you think that there is a good reason, you should try to do this in order to get some kind of result. You should ask for the desired result for help, because your plan is bad.

+14
source share

Like other posters, I'm not quite sure what you want to achieve. Maybe exceptions are what you need? You can use exceptions to remove the dependencies of your dependencies - if they are not needed for some reason.

  <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> 
+2
source share

All Articles