user1589188
user1589188

Reputation: 5736

Maven3 excludes unnecessary dependencies inherited from parent pom for distribution?

Hi this is what I want to achieve: To distribute one of the modules of a bigger project by itself with all the dependencies required by that module, rather than all of the dependencies inherited from its parent pom.

In order to achieve that, I put includes in the assembly plugin of the distributing module, which excludes everything else not in the includes.

Please let us know if this is the practice or a hack? Would accept a different solution if that makes more sense, but there will be a parent pom and the parent pom will have a lot of other dependencies not related to this module, otherwise tell us if we should not put dependencies in parent pom but to put them in individual module poms

Upvotes: 0

Views: 7342

Answers (1)

Adrian Shum
Adrian Shum

Reputation: 40076

otherwise tell us if we should not put dependencies in parent pom but to put them in individual module poms

My suggestion is similar to this. Dependencies in Parent POM is supposed to be mostly used by inherited project. You shouldn't blindly put everything possible dependencies in you parent. It is ruining the dependency management mechanism.

However, personally I usually put every possible dependencies as dependencyManagement in parent POM instead (Normally when I have a sligntly bigger project, I make it multi-module, and one of the modules is parent for the project). This help to avoid individual module of same project defining different version of same dependency. By doing so, each module (in most case) only declare the group ID and artifact ID of the dependencies it needs. (I hope your reason of putting every dependency in parent is related)

More information about managing dependencies in parent POM


Update: To answer OP's question in comment:

Thanks for the suggestion. Parent POM can be cleaned up a bit I guess. But what if the unnecessary dependencies are coming from a 3rd party library and we don't really need/want (because of different versions?) all but just a few of them?

That's a totally different story. In some cases we want to control how dependencies are looked up transitively. There are some techniques you would want to know:

When you declare dependencies, you can declare excludes so it excludes some transitive dependencies, like this:

<dependency>
    <groupId>foo</groupId>
    <artifactId>foo-core</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>bar</group>
            <artifactId>bar-dep</artifactId>
        </exclusion>
    </exclusions>
</dependency>

One of the beauties of using dependencyManagement is, if you put the above stuff in parent POM as dependencyManagement, when several modules need to use foo:foo-core, they simply declare groupId and artifactId, without needs to repeat those lengthy exclusions

Another way is, for example, you still want bar:bar-dep in the above example, it is just that you need a version different from that looked up transitively, then you can simply declare the correct version you want in your POM, and Maven is going to use the "nearest" version, as described here

Still other tricks of excluding by depending on an empty dependency, as described in comment in question

Upvotes: 1

Related Questions