如何从build.gradle中包含的外部jar的可传递依赖项中排除包?

如何从build.gradle中包含的外部jar的可传递依赖项中排除包?,gradle,build.gradle,Gradle,Build.gradle,我在build.gradle中包含了以下依赖项: testCompile group: 'com.xebialabs.restito', name: 'restito', version:'0.5.1' 但在这个jar中,还包含了其他依赖项,如jersey-core-1.18.3.jar 现在我想要这个jersey jar,但它还有一个包javax.ws.rs.core,其中包含与build.gradle中显式包含的最新版本javax.ws.rs.core冲突的类 是否有任何方法可以仅从可传递

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

testCompile group: 'com.xebialabs.restito', name: 'restito', version:'0.5.1'
但在这个jar中,还包含了其他依赖项,如jersey-core-1.18.3.jar

现在我想要这个jersey jar,但它还有一个包javax.ws.rs.core,其中包含与build.gradle中显式包含的最新版本javax.ws.rs.core冲突的类

是否有任何方法可以仅从可传递依赖项中排除特定的包,而不排除整个依赖项。我是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.rsapi: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()
方法。在我的项目中,
响应
的依赖关系始终会解决为早期版本。

引用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'
    }
}
现在,上面的文件as正在运行
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。我尝试了类似的解决方案,但没有成功。在我的问题中,jersey core有一个内部包结构javax.ws.rs.core,类似地,rs-api2.0也有相同的结构。javax.ws.rs.core。我希望我的回答是稍后的。查看MavenCentral中的pom文件,我不确定我是否发现了您希望排除的确切JAR。你能在你的问题中添加依赖项树吗?对于testCompile配置,您只需运行
gradlew dependencies--configuration testCompile
,粘贴它的输出并标记要跳过下载的工件。我已经更新了我的问题,并提供了更多详细信息。谢谢