0

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

user1589188
  • 5,316
  • 17
  • 67
  • 130
  • 1
    Why are you inheriting that parent POM in first place if you don't want its dependencies? Dependencies in parent POM is supposed to be used by all inherited project. I'd rather refactor the parent POMs. Anyway, an alternative for you: http://stackoverflow.com/a/7898553/395202 – Adrian Shum Jun 02 '15 at 02:24

1 Answers1

1

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

Community
  • 1
  • 1
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • 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? – user1589188 Jun 02 '15 at 03:00
  • How can you tell if both exclusion and specify pom defined then which one takes precedence? And among the exclusion and the stub jar trick, how would you choose one or the other? – user1589188 Jun 02 '15 at 03:41
  • there is nothing about precedence for "exclusion" vs "specifying version in POM". Version resolve logic only applies to multiple versions of dependencies happens transitively so Maven needs to decide which version to use (they are now using "nearest" one). If you excluded, then there is no conflicting versions of same dependency. Your explicit dependency declaration in POM is only a new dependency, which has nothing to do with the transitive one (coz there is no transitive one) – Adrian Shum Jun 02 '15 at 03:45