是否可以阻止Gradle添加排除的传递依赖项



我有一个使用Gradle 5.6构建的Java库,其中抑制了一些传递依赖项

api('org.springframework.boot:spring-boot-starter-web') {
exclude module: 'spring-boot-starter-logging'
exclude module: 'spring-boot-starter-tomcat'
}

当我将其发布到Maven repo时,我会得到POM.xml的相应部分

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>*</groupId>
</exclusion>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>

但当我使用Gradle 5.6 将我的库添加为依赖项时

dependencies {
implementation 'my.group:my.lib:1.0.0'
}

我看到排除的依赖项(例如spring-boot-starter-tomcat(出现在我的compileClasspath配置中。有没有办法一劳永逸地排除它,或者我应该在所有手动使用我的库的项目中都这样做?

如文档中所述(强调矿(:

排除特定的可传递依赖项并不保证它不会出现在给定配置的依赖项中。例如,其他一些没有任何排除规则的依赖项可能会引入完全相同的传递依赖项为了保证从整个配置中排除可传递依赖项,请使用每个配置的排除规则:configuration.getExcludeRules((。事实上,在大多数情况下,配置每个依赖项排除的实际意图实际上是从整个配置(或类路径(中排除依赖项。

您可以将规则应用于所有配置,而不是为每个配置指定排除规则:

// Kotlin DSL
configurations.all {
exclude(mapOf("module" to "spring-boot-starter-logging"))
exclude(mapOf("module" to "spring-boot-starter-tomcat"))
}

最新更新