17

We have a maven module that is set up as so:

a (parent)
  -> b (submodule)
  -> c (submodule)
  -> d (submodule)

This list of submodules is set to grow as time goes on (to a list of 20 or so). We have another module that will include as dependencies all submodules of a. Is there a neat way of doing this, rather than having to manually keep the submodule list in sync with the dependencies list. I.e. is there any way of including a and all submodules as a dependency?

smauel
  • 665
  • 1
  • 8
  • 14

3 Answers3

6

You have a few choices here:

  1. No change. List all dependencies in each pom. However, if they have a common parent, you can use dependencyManagement in the parent pom to set the versions for the different dependencies. In the child poms, you do not need to list the version. See this section from Maven By Example. A downside of this approach is that you have to re-list the same dependencies over and over.
  2. Create a parent pom which lists all shared dependencies. See an example here. A downside here is you are restricting all projects that want to take advantage of this to use a parent project when they may need to use another parent project for some reason.
  3. Wait for Maven mixins, which last I heard were still not ready.
  4. Rethink your design. Does it make sense for projects to depend on so many different modules and interfaces? To reduce coupling, you may want to create one interface that these new projects can use. Several open source multi-module projects, such as Apache Axis2, follow this pattern. One module contains your 20 dependencies and exposes an interface which the new modules can call. The new modules can just list that one main module as a dependency and all of the 20 dependencies are pulled in as transitive dependencies.

I think choice #4 is probably right, but I am not familiar enough with your situation.

Community
  • 1
  • 1
BennyMcBenBen
  • 1,438
  • 2
  • 20
  • 37
  • I have similar problem. approach #4 seems a good option. It would be helpful if have/ you can point me to any example for approach #4? – Pranav Maniar Jan 08 '16 at 14:57
2

Design considerations aside, this is easily done - simply include <type>pom</type> in your dependency pointing at the parent pom. E.g:

<dependency>
    <groupId>my.group.id</groupId>
    <artifactId>a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>pom</type>
</dependency>
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
0

The only way dependencies get "automatically" included is via the transitive dependency mechanism, and it only works by pulling in dependencies of dependencies, so unless you have a pom that depends on all the submodules, no, you won't get them without listing them all out. A need to do this points to a flaw in your project design. If the modules are "all or none", then they're probably not separate modules.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199