Mehdi
Mehdi

Reputation: 1531

Exclude maven sub modules dependencies from top level module

I created a maven parent project with three modules :

Here is a snippet from the parent pom.xml :

<groupId>org.mygroup</groupId>
<artifactId>myArtifact</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
  <module>data-layer-module</module>
  <module>data-service-module</module>
  <module>web-module</module>
</modules> 

The data-layer-module pom.xml contains no dependencies to the other modules (it contains only its external dependencies).

The data-service-module has a dependency in its pom.xml to the data-layer-module :

<dependencies>
      <dependency>
          <groupId>org.mygroup</groupId>
          <artifactId>data-layer-module</artifactId>
          <version>1.0-SNAPSHOT</version>
      </dependency>
</dependencies>

And the web-module has a dependency to the data-service-module :

<dependency>
    <groupId>org.mygroup</groupId>
    <artifactId>data-service-module</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Everything is fine but I don't want the web-module to have access to the data-layer-module. I need to enforce the three layer web-service-data model. Which is not the case with this configuration.
I obviously tried manually excluding it from the web-module pom.xml :

<dependency>
    <groupId>org.mygroup</groupId>
    <artifactId>data-service-layer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>org.mygroup</groupId>
            <artifactId>data-layer-module</artifactId>
        </exclusion>
    </exclusions>
</dependency>  

But this causes compilation error since the data-service-module can't find its data-layer-module dependency.

How could this configuration be done ?

Upvotes: 1

Views: 1616

Answers (1)

vikingsteve
vikingsteve

Reputation: 40378

If the web module is going to run in the same jvm as the data-service-layer, then a transient dependency to the data-layer-module is necessary - this is why the exclusion you added casuses the application to fail.

You could consider making a simple api, for example data-service-layer-api, which obviously does not depend on data-layer-module, and is implemented correctly by data-service-layer.

You can still use a multi-module maven project, but now you will have 2 artifacts - a web module, and a data-service, which must be deployed separated.

It's basically a tradeoff between strict dependency analysis and project complexity.

Upvotes: 1

Related Questions