java 从 Gradle 依赖中排除包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30942825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude Package From Gradle Dependency
提问by matheeeny
I'm experiencing an issue where multiple versions of the same class are showing up in my classpath. The class in question is javax.ws.rs.core.UriBuilder
. The version I want to use is brought in by javax.ws.rs:javax.ws.rs-api:2.0.1
. However, we also use the Jira rest client library which has a dependency on the older version of jersey (com.sun.jersey:jersey-core
) which has included the java.ws packages bundled in it's jar.
我遇到了一个问题,即我的类路径中出现了同一个类的多个版本。有问题的班级是javax.ws.rs.core.UriBuilder
. 我要使用的版本是由javax.ws.rs:javax.ws.rs-api:2.0.1
. 但是,我们也使用 Jira rest 客户端库,它依赖于旧版本的 jersey ( com.sun.jersey:jersey-core
),其中包含捆绑在其 jar 中的 java.ws 包。
Here is an example snippet from the build file:
这是构建文件中的示例片段:
dependencies {
compile 'com.atlassian.jira:jira-rest-java-client-core:2.0.0-m31'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'org.glassfish.jersey.core:jersey-client:2.17'
}
I can't remove com.sun.jersey:jersey-core
as it uses different package name from the new version and would cause class def not found exceptions in the Jira client.
我无法删除,com.sun.jersey:jersey-core
因为它使用了与新版本不同的包名,并且会导致在 Jira 客户端中出现 class def not found 异常。
As far as I can tell, my options at this point are:
据我所知,此时我的选择是:
- Revert to using Jersey 1.x and it's implementation of jsr311
- Somehow have gradle exclude the
javax.ws
package from the old jersey client.
- 恢复使用 Jersey 1.x 和它的 jsr311 实现
- 不知何故,gradle
javax.ws
从旧的球衣客户端中排除了这个包。
I'd like to keep using the newer version of jersey so #2 would be my ideal solution but I'm not sure if it's even possible. Does anyone know how to go about this? If that's not possible, I'm open to other suggestions.
我想继续使用较新版本的球衣,所以 #2 将是我理想的解决方案,但我不确定它是否可能。有谁知道如何解决这个问题?如果这是不可能的,我愿意接受其他建议。
采纳答案by matheeeny
I found out that com.sun.jersey:jersey-core:1.19 doesn't bundle the javax.ws.rs class files and instead lists them as a compile-time dependency. Adding this snippet to my build.gradle fixed the issue.
我发现 com.sun.jersey:jersey-core:1.19 没有捆绑 javax.ws.rs 类文件,而是将它们列为编译时依赖项。将此代码段添加到我的 build.gradle 解决了该问题。
configurations.all {
resolutionStrategy {
// For a version that doesn't package javax.ws
force 'com.sun.jersey:jersey-core:1.19'
}
}
回答by RaGe
You can exclude an transitive dependency module like this:
您可以像这样排除传递依赖模块:
compile ('org.glassfish.jersey.core:jersey-client:2.17') {
exclude group: 'javax.ws.rs'
exclude module: 'javax.ws.rs-api'
}
ref: 50.4.7 here
参考:50.4.7 这里