views:

62

answers:

3

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.com:
<dependencies>
    <dependency>c</dependency>
</dependencies>

Expected result:

In a.pom: 
<dependencies>
    <dependency>b</dependency>
</dependencies>
A: 

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>
lexicore
A: 

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.

Zac Thompson
note that you should run `mvn install` before `dependency:analyze` because the analysis is done on the compiled artifact
Zac Thompson
+2  A: 

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

ithkuil
You are right. I have to cleanup the poms, the dependencies are not so well declared because the programmers are not very familiar with Maven tools and software engineering. The dependencies are mostly managed by IDEs. It's also valuable to generate a minimized dependency graph for illustration purpose.
谢继雷