排除Gradle插件的传递依赖

时间:2019-06-02 07:04:23

标签: java gradle

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

compile('com.example.m:m:1.0') {
     exclude group: 'org.unwanted', module: 'x'
  }

在使用插件的情况下,我们将如何解决他:

apply: "somePlugin"

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

2 个答案:

答案 0 :(得分:1)

您可以在应用插件后(例如,从单个配置或所有配置)删除依赖项。 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

答案 1 :(得分:1)

您可以通过以下方式操纵buildscript本身的类路径:

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
        }
    }
}