Import Dependency Management with Exclusion

2020-08-09 10:47发布

问题:

I have a beautiful BOM with a lot of dependencies in its dependencyManagement section and I would like to create another BOM that imports all that dependencies except one. I tried doing this:

... in my dependencyManagement section
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>${spring-boot-version}</version>
    <type>pom</type>
    <scope>import</scope>

    <exclusions>
        <exclusion>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        </exclusion>
    </exclusions>
</dependency>
...

The POM is formally correct and everything compiles. But the exclusion is simply ignored. What am I missing? Is this approach correct?

I'm using Maven 3+.

回答1:

Exclusion at import won't work, try excluding it from the actual user of the dependency



回答2:

Exclusions are still not implemented for dependencyManagement as of current maven 3.6.3. However you can include a project specific "Bill Of Materials" (BOM) as the first dependency in the dependencyManagement section, i.e.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>my-group</groupId>
            <artifactId>my-group-project-bom</artifactId>
            <version>${project.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <type>pom</type>
            <version>${spring-boot.version}</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

You can then specify all the necessary artifact versions in your project BOM which will take precedence over the spring-boot dependency versions.



标签: maven