bobsyouruncle
bobsyouruncle

Reputation: 137

Maven - depend on multi-module aggregator project

I've got a multi-module maven project, with a structure like:

projectA-parent
  - module-1
  - module-2

And I have another project where I want to bring in all the modules in projectA-parent as runtime dependencies (it's a Spring application, and each module in projectA-parent contains Spring services that I want to be able to autowire).

What I'd like to do is something like

<dependency>
    <groupId>projectA-group</groupId>
    <artifactId>projectA-parent</artifactId>
    <scope>runtime</scope>
</dependency>

so that if I add another module to projectA-parent it is automatically brought in as a runtime dependency (i.e., I don't want to have to add each new module as a dependency in my Spring application as I add them). Is such a thing possible?

Upvotes: 2

Views: 1882

Answers (2)

ninj
ninj

Reputation: 1549

I guess I'd just add another module that referred to the other modules in your project, e.g.:

projectA-parent
  - module-1
  - module-2
  - module-deps

with module-deps as a jar or pom that depends on module-1 and module-2. You'll have to update module-deps as you add more modules, but at least it's only in one place.

Upvotes: 3

RITZ XAVI
RITZ XAVI

Reputation: 3799

You will have to use

<dependencies>
  <dependency>
     <groupId>projectA-parent-groupId</groupId>
     <artifactId>projectA-parent-artifactId</artifactId>
     <type>pom</type>
  </dependency>
</dependencies>

This will transitively add all dependencies declared in com.my:commons-deps to your current POM.

Using

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

works as a simple 'include' of artifacts versions in your dependency management. Thus, it won't add any dependency in your project.

UPDATE:

Another aprroach would be to use a BOM (Bill of Materials). Check this link for the usage of BOM. It is hidden somewhere at the bottom.

You can create a BOM that lists all your modules as dependencies and then you can include the BOM into your pom.xml like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>your_bom_group_id</groupId>
            <artifactId>your_bom_artifact_id</artifactId>
            <version>you_bom_version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Upvotes: 3

Related Questions