【问题标题】:How to exclude a package from a transitive dependency of external jar included in build.gradle?如何从 build.gradle 中包含的外部 jar 的传递依赖中排除包?
【发布时间】:2015-05-14 12:28:58
【问题描述】:

我在 build.gradle 中包含了以下依赖项:

testCompile group: 'com.xebialabs.restito', name: 'restito', version:'0.5.1'

但是对于这个 jar,其他依赖项,如 jersey-core-1.18.3.jar 也会包含在内。

现在我想要这个球衣 jar,但它也有一个包 javax.ws.rs.core,其中包含与我的最新版本 javax.ws.rs.core 冲突的类,这些类明确包含在构建中.gradle.

有什么方法可以只从传递依赖项中排除特定包,而不是整个依赖项。我是 gradle 的新手,所以如果使用了任何不正确的术语,请纠正我。

当我运行以下命令时:

gradlew dependencies --configuration testCompile

它给了我以下依赖树。这里只显示相关部分

testCompile - Compile classpath for source set 'test'.

+--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
|    +--- org.apache.cxf:cxf-core:3.0.3 (*)
|    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3 (*)
+--- org.apache.cxf:cxf-rt-rs-service-description:3.0.0-milestone1
|    \--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.0-milestone1 -> 3.0.3 (*)
+--- org.codehaus.jackson:jackson-jaxrs:1.9.13
|    +--- org.codehaus.jackson:jackson-core-asl:1.9.13
|    \--- org.codehaus.jackson:jackson-mapper-asl:1.9.13
|         \--- org.codehaus.jackson:jackson-core-asl:1.9.13
+--- org.codehaus.jackson:jackson-mapper-asl:1.9.12 -> 1.9.13 (*)
+--- com.fasterxml.jackson.core:jackson-databind:2.4.1.2
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
|    \--- com.fasterxml.jackson.core:jackson-core:2.4.1.1
\--- com.xebialabs.restito:restito:0.5.1
     +--- org.slf4j:slf4j-api:1.7.5 -> 1.7.7
     +--- org.mockito:mockito-core:1.10.17
     |    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     |    \--- org.objenesis:objenesis:2.1
     +--- org.apache.mina:mina-core:2.0.4
     |    \--- org.slf4j:slf4j-api:1.6.1 -> 1.7.7
     +--- org.glassfish.grizzly:grizzly-http-server:2.3.17
     |    \--- org.glassfish.grizzly:grizzly-http:2.3.17
     |         \--- org.glassfish.grizzly:grizzly-framework:2.3.17
     +--- junit:junit:4.12
     |    \--- org.hamcrest:hamcrest-core:1.3
     \--- com.sun.jersey:jersey-grizzly2:1.18.3
          +--- org.glassfish.grizzly:grizzly-http:2.2.16 -> 2.3.17 (*)
          +--- org.glassfish.grizzly:grizzly-http-server:2.2.16 -> 2.3.17 (*)
          \--- com.sun.jersey:jersey-server:1.18.3
               \--- com.sun.jersey:jersey-core:1.18.3

(*) - dependencies omitted (listed previously)

现在javax.ws.rs:javax.ws.rs-api:2.0.1中的Response类在javax.ws.rs.core包下。同样,在 javax.ws.rs.core 包的 com.sun.jersey:jersey-core:1.18.3 中还有另一个 Response 类。但后者包含早期版本,没有在 rs-api 2.0 中引入的 readEntity() 方法。我的项目中 Response 的依赖关系总是被解析为早期版本。

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    引用关于Excluding transitive dependencies的gradle用户指南部分:

    您可以通过配置或依赖排除传递依赖:

    示例 50.14

    configurations {
        compile.exclude module: 'commons'
        all*.exclude group: 'org.gradle.test.excludes', module: 'reports'
    }
    
    dependencies {
        compile("org.gradle.test.excludes:api:1.0") {
        exclude module: 'shared'
        }
    }
    

    回到您的情况,您可以简单地采用上述方法之一,并将示例中的模块替换为您要排除的模块,例如

    configurations {
        testCompile.exclude group: 'javax.ws.rs', module: 'jsr311-api'
    }
    

    更新(根据提供的其他详细信息)

    您要做的就是在脚本中添加以下行

    configurations {
      testCompile.exclude group: 'com.sun.jersey', module: 'jersey-core'
    }
    

    考虑以下最小的build.gradle 脚本:

    应用插件:'java'

    repositories {
        mavenCentral()
    }
    
    configurations {
      // Excluding com.sun.jersey:jersey-core:1.18.3, option #1: 
      // compile.exclude group: 'com.sun.jersey', module: 'jersey-core'
    }
    
    dependencies {
        compile "org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3"
        compile ("com.sun.jersey:jersey-grizzly2:1.18.3") {
          // Excluding com.sun.jersey:jersey-core:1.18.3, option #2: 
          // exclude group: 'com.sun.jersey', module: 'jersey-core'
        }
    }
    

    现在,上面的文件正在运行gradlew dependencies --configuration testCompile,您将获得下一个依赖关系树:

    +--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
    |    +--- org.apache.cxf:cxf-core:3.0.3
    |    |    +--- org.codehaus.woodstox:woodstox-core-asl:4.4.1
    |    |    |    \--- org.codehaus.woodstox:stax2-api:3.1.4
    |    |    \--- org.apache.ws.xmlschema:xmlschema-core:2.1.0
    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
    |    +--- javax.annotation:javax.annotation-api:1.2
    |    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3
    |         \--- org.apache.cxf:cxf-core:3.0.3 (*)
    \--- com.sun.jersey:jersey-grizzly2:1.18.3
         +--- org.glassfish.grizzly:grizzly-http:2.2.16
         |    \--- org.glassfish.grizzly:grizzly-framework:2.2.16
         +--- org.glassfish.grizzly:grizzly-http-server:2.2.16
         |    +--- org.glassfish.grizzly:grizzly-http:2.2.16 (*)
         |    \--- org.glassfish.grizzly:grizzly-rcm:2.2.16
         |         \--- org.glassfish.grizzly:grizzly-framework:2.2.16
         \--- com.sun.jersey:jersey-server:1.18.3
              \--- com.sun.jersey:jersey-core:1.18.3
    

    请注意,com.sun.jersey:jersey-core:1.18.3 被列为传递依赖项。现在,取消注释任一排除选项并重新运行相同的命令,您将获得以下包含此模块的输出:

    +--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
    |    +--- org.apache.cxf:cxf-core:3.0.3
    |    |    +--- org.codehaus.woodstox:woodstox-core-asl:4.4.1
    |    |    |    \--- org.codehaus.woodstox:stax2-api:3.1.4
    |    |    \--- org.apache.ws.xmlschema:xmlschema-core:2.1.0
    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
    |    +--- javax.annotation:javax.annotation-api:1.2
    |    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3
    |         \--- org.apache.cxf:cxf-core:3.0.3 (*)
    \--- com.sun.jersey:jersey-grizzly2:1.18.3
         +--- org.glassfish.grizzly:grizzly-http:2.2.16
         |    \--- org.glassfish.grizzly:grizzly-framework:2.2.16
         +--- org.glassfish.grizzly:grizzly-http-server:2.2.16
         |    +--- org.glassfish.grizzly:grizzly-http:2.2.16 (*)
         |    \--- org.glassfish.grizzly:grizzly-rcm:2.2.16
         |         \--- org.glassfish.grizzly:grizzly-framework:2.2.16
         \--- com.sun.jersey:jersey-server:1.18.3
    

    【讨论】:

    • 谢谢@Amnon。我尝试了类似的解决方案,但没有奏效。在我的问题中,球衣核心具有 javax.ws.rs.core 的内部包结构,类似地,RS-api2.0 具有相同的结构。 javax.ws.rs.core。我希望我的回复稍后再回复。
    • 查看 MavenCentral 中的 pom 文件我不确定是否找到了您想要排除的确切 jar。您能否将依赖关系树添加到您的问题中?对于 testCompile 配置,您只需运行 gradlew dependencies --configuration testCompile,粘贴它的输出并标记您要跳过下载的工件。
    • 我更新了我的问题,提供了更多细节。谢谢
    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多