Olivier J.
Olivier J.

Reputation: 3175

Use transitive dependencies and exclusions with Maven

I have this maven hierachy :

sim-java
   ejb
   web
   log4j
   ...

ejb, web and log4j are modules of sim-java and each of these modules refer to sim-java by parent tag.

I would like to create a log4j module and include it as dependency in sim-java so log4j will be present as dependency in ejb and web modules.

Basically, my pom.xml inside sim-java contains this :

<dependencies>
    <dependency>
        <groupId>com.sim</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

(scope is provided as I have an ear module which will include com.sim:log4j in ear file).

Now, my pom.xml inside my log4j module is this one :

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>

I have this build error :

30/10/12 18:56:07 CET: Build errors for log4j; org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project log4j: Could not resolve dependencies for project com.sim:log4j:jar:0.0.1-SNAPSHOT: Could not find artifact com.sim:log4j:jar:0.0.1-SNAPSHOT

I thought that there was some dependency problem since sim-java use com.sim.log4j as transitive dependency, so I tested that inside log4j pom.xml :

    <dependencies>
    <dependency>
        <groupId>com.sim</groupId>
        <artifactId>sim-java</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>com.sim</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>

30/10/12 18:59:22 CET: [WARN] The POM for com.sim:log4j:jar:0.0.1-SNAPSHOT is missing, no dependency information available 30/10/12 18:59:22 CET: Missing artifact com.sim:log4j:jar:0.0.1-SNAPSHOT:provided 30/10/12 18:59:22 CET: Missing artifact com.sim:log4j:jar:0.0.1-SNAPSHOT:provided 30/10/12 18:59:22 CET: Missing artifact com.sim:log4j:jar:0.0.1-SNAPSHOT:provided

Perhaps including com.sim.log4j inside modules (ejb, web etc...) would work but I want to use transitive dependency via sim-java project.

How to do this please ?

Olivier

Upvotes: 1

Views: 1119

Answers (1)

maba
maba

Reputation: 48105

You cannot have the dependency on com.sim:log4j in the parent pom. That dependency will be inherited in the children meaning that com.sim:log4j will depend on itself.

Instead create a <dependencyManagement/> in the parent pom and then declare the use of com.sim:log4j in the children that needs it, here it is web and ejb.

Parent pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.sim</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Web and Ejb pom:

<dependencies>
    <dependency>
        <groupId>com.sim</groupId>
        <artifactId>log4j</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Edit

You can still have a transitive dependency by only having the dependency on the log4j module from the ejb project. The web project will then have a dependency on the ejb project and the log4j will be a transitive dependency.

Upvotes: 2

Related Questions