【发布时间】:2022-04-24 05:01:51
【问题描述】:
在 Gradle 中排除传递依赖非常简单:
compile('com.example.m:m:1.0') {
exclude group: 'org.unwanted', module: 'x'
}
我们将如何解决使用插件的情况:
apply: "somePlugin"
当获得依赖时,我们意识到插件带来了它自己的一些传递依赖?
【问题讨论】:
在 Gradle 中排除传递依赖非常简单:
compile('com.example.m:m:1.0') {
exclude group: 'org.unwanted', module: 'x'
}
我们将如何解决使用插件的情况:
apply: "somePlugin"
当获得依赖时,我们意识到插件带来了它自己的一些传递依赖?
【问题讨论】:
您可以在应用插件后(从单个配置或所有配置中)删除依赖项,例如。 compile.exclude。请注意,compile 解析为“配置”;请参阅Configuration.exclude 的 javadocs。
编辑
请注意,如果配置已解决,排除依赖项可能会失败。
示例脚本
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencies {
compile 'junit:junit:4.12'
compile 'ant:ant:1.6'
compile 'org.apache.commons:commons-lang3:3.8'
}
// remove dependencies
configurations.all {
exclude group:'junit', module:'junit'
}
configurations.compile {
exclude group:'org.apache.commons', module: 'commons-lang3'
}
println 'compile deps:\n' + configurations.compile.asPath
【讨论】:
您可以通过以下方式操作构建脚本本身的类路径:
buildscript {
configurations {
classpath {
exclude group: 'org', module: 'foo' // For a global exclude
}
}
dependencies {
classpath('org:bar:1.0') {
exclude group: 'org', module: 'baz' // For excluding baz from bar but not if brought elsewhere
}
}
}
【讨论】:
这是强制您的项目严格使用特定版本的 build.gradle.kts 的另一种方法
val grpcVersion = "1.45.1"
implementation("io.grpc:grpc-stub") {
version {
strictly(grpcVersion)
}
}
更多信息可以在 gradle 文档中找到:https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html
【讨论】: