Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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>
like image 910
Xiè Jìléi Avatar asked Sep 30 '10 09:09

Xiè Jìléi


People also ask

How do you remove a transitive dependency?

If a transitive dependency exists, we remove the transitively dependent attribute(s) from the relation by placing the attribute(s) in a new relation along with a copy of the determinant.

How do you exclude transitive dependency of transitive dependency?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml.

How does Maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.


1 Answers

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:

  1. in project a , run mvn dependency:tree -Dverbose=true. This will show you a complete tree of all dependencies considered by Maven for your project.
  2. Look at the output of step 1 and make a list of all dependencies that are shown at more than one level deep (some of them will probably be duplicates).
  3. Edit your pom.xml file in whatever editor you like and remove any dependencies that match the list you created in step 2.

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.

like image 120
Zac Thompson Avatar answered Nov 13 '22 07:11

Zac Thompson