首页 文章

使用Gradle时如何排除传递依赖的所有实例?

提问于
浏览
76

我的gradle项目使用 application 插件来构建一个jar文件 . 作为运行时传递依赖项的一部分,我最终会进入 org.slf4j:slf4j-log4j12 . (它也是's referenced as a sub-transitive dependency in at least 5 or 6 other transitive dependencies - this project is using spring and hadoop, so everything but the kitchen sink is getting pulled in... no wait... that' s :)) .

我想从我的内置jar中全局排除 slf4j-log4j12 jar . 所以我试过这个:

configurations {
  runtime.exclude group: "org.slf4j", name: "slf4j-log4j12"
}

但是,这似乎排除了包括 slf4j-api 在内的 all org.slf4j 文物 . 在调试模式下运行时,我看到以下行:

org.slf4j#slf4j-api is excluded from com.pivotal.gfxd:gfxd-demo-mapreduce:1.0(runtime).
org.slf4j#slf4j-simple is excluded from com.pivotal.gfxd:gfxd-demo-mapreduce:1.0(runtime).
org.slf4j#slf4j-log4j12 is excluded from org.apache.hadoop:hadoop-common:2.2.0(runtime).

我不想查找每个 slf4j-log4j12 传递依赖的源,然后在我的 dependencies 块中有单独的 compile foo { exclude slf4j... } 语句 .

Update:

我也试过这个:

configurations {
  runtime.exclude name: "slf4j-log4j12"
}

最终从构建中排除 everything !好像我指定了 group: "*" .

Update 2:

我正在使用Gradle版本1.10 .

5 回答

  • 22

    啊,以下工作并做我想要的:

    configurations {
      runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
    }
    

    似乎Exclude Rule只有两个属性 - groupmodule . 但是,上述语法不会阻止您将任意属性指定为谓词 . 尝试从单个依赖项中排除时,您无法指定任意属性 . 例如,这失败了:

    dependencies {
      compile ('org.springframework.data:spring-data-hadoop-core:2.0.0.M4-hadoop22') {
        exclude group: "org.slf4j", name: "slf4j-log4j12"
      }
    }
    

    No such property: name for class: org.gradle.api.internal.artifacts.DefaultExcludeRule
    

    因此,即使您可以使用 group:name: 指定依赖项,也无法使用 name: 指定排除项!?!

    也许是一个单独的问题,但是什么 exactly 是一个模块呢?我可以理解groupId:artifactId:version的Maven概念,我理解它转换为group:name:Gradle中的版本 . 但是,我怎么知道特定Maven工件属于哪个模块(以gradle-speak方式)?

  • 107

    你的方法是正确的 . (根据具体情况,您可能希望使用 configurations.all { exclude ... } . )如果这些排除实际上排除了多个依赖项(我在使用它时没有注意到),请在http://forums.gradle.org提交一个错误,理想情况下是一个可重现的示例 .

  • 14

    要全局排除一个或多个库,请将以下内容添加到build.gradle中

    configurations.all {
       exclude group:"org.apache.geronimo.specs", module: "geronimo-servlet_2.5_spec"
       exclude group:"ch.qos.logback", module:"logback-core"
    }
    

    现在,排除块有两个属性 groupmodule . 对于那些来自maven背景的人, groupgroupId 相同, moduleartifactId 相同 . 示例:要排除com.mchange:c3p0:0.9.2.1以下应该是排除块

    exclude group:"com.mchange", module:"c3p0"
    
  • 0

    在下面的例子我排除

    spring-boot-starter-tomcat

    compile("org.springframework.boot:spring-boot-starter-web") {
         //by both name and group
         exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' 
    }
    
  • 25

    我正在使用spring boot 1.5.10并试图排除logback,上面给出的解决方案效果不好,我使用的是配置

    configurations.all {
        exclude group: "org.springframework.boot", module:"spring-boot-starter-logging"
    }
    

相关问题