Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can child poms inherit dependency exclusions defined in the parent pom?

Tags:

maven

I'm not sure if this is support by Maven or not. I appreciate any help I can get.

I have a parent pom that defines a dependency and an exclusion. I can't change the parent pom:

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0</version>
    <exclusions>
        <!-- this exclusion needs to be inherited by all children -->
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Then in the child pom, I have a need to exclude a different dependency from that same dependency in the parent. Like so

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0</version>
    <exclusions>
        <!-- this exclusion is just for the child -->
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

But if I do that, the child will have the slf4j jar excluded (correctly), but it will NOT have the spring-cloud-config-server jar excluded, unless if I restate the parent exclusion in the child declaration of that dependency.

I realize I could just copy it, but that's messy, and I realize that it would be easy to push the child's exclusion up to the parent, but then I'd be forcing that exclusion on all the other children.

What I'd like is for Maven to merge the dependency exclusion information when the same dependency is declared differently in the parent and child.

Is that possible?

like image 562
Daniel Hannum Avatar asked Oct 03 '16 15:10

Daniel Hannum


1 Answers

In your parent pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>foo</groupId>
            <artifactId>bar</artifactId>
            <version>1.0</version>
            <exclusions>
                <!-- this exclusion needs to be inherited by all children -->
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-config-server</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

In your child pom:

<dependencies>
    <dependency>
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <exclusions>
            <!-- this exclusion is just for the child -->
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
like image 121
Diane Duan Avatar answered Nov 15 '22 09:11

Diane Duan