排除 Gradle 插件的传递依赖

Cri*_*ina 5 java gradle

在 Gradle 中排除传递依赖非常简单:

compile('com.example.m:m:1.0') {
     exclude group: 'org.unwanted', module: 'x'
  }
Run Code Online (Sandbox Code Playgroud)

我们将如何解决我们使用插件的情况:

apply: "somePlugin"
Run Code Online (Sandbox Code Playgroud)

当获取依赖项时,我们意识到该插件带来了自己的一些传递依赖项?

Lou*_*met 11

您可以通过以下方式操作构建脚本本身的类路径:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*ele 6

您可以在应用插件后(从单个配置或所有配置)删除依赖项,例如使用。compile.exclude. 请注意,compile解析为“配置”;请参阅Configuration.exclude 中的 javadoc 。

编辑

请注意,如果配置已经解决,排除依赖项可能会失败。


示例脚本

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
Run Code Online (Sandbox Code Playgroud)