2015-05-14 26 views
5

我已经包含在我的build.gradle以下依赖性:如何从包含在build.gradle中的外部jar的传递依赖项中排除程序包?

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

但这个罐子其他依赖像球衣核心 - 1.18.3.jar也得到包括在内。

现在我想这件球衣罐子,但这样做也有一个包javax.ws.rs.core它包含了我的最新javax.ws.rs.core的版本冲突的build.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) 

现在类Responsejavax.ws.rs:javax.ws.rs-api:2.0.1是包javax.ws.rs.core下。类似的还有另一种Response类别com.sun.jersey:jersey-core:1.18.3 in包javax.ws.rs.core。但后者包含的早期版本没有在rs-api 2.0中引入的readEntity()方法。在我的项目中对Response的依赖关系会始终解析为早期版本。

回答

7

援引gradle这个用户引导部约Excluding transitive dependencies

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

实施例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 
+0

谢谢@Amnon。我尝试了类似的解决方案,但没有工作。在我的问题中,泽西核心具有javax.ws.rs.core的内部包结构,并且类似地,RS-api2.0具有相同的结构。 javax.ws.rs.core。我希望我的回应稍后再做。 –

+0

查看MavenCentral中的pom文件我不确定是否发现了您想要排除的确切瓶子。你可以添加你的问题依赖关系树?对于testCompile配置,您可以简单地运行'gradlew dependencies --configuration testCompile',粘贴它的输出并标记您要跳过下载的工件。 –

+0

我已更新我的问题,并提供更多详细信息。谢谢 –

相关问题