Java Gradle 排除依赖项中的特定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23640030/
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
Gradle exclude specific files inside dependency
提问by bhumphrey
I was wondering if there was anyway to exclude specific files,that are inside a dependency (not a transitive dependency), from being downloaded.
我想知道是否有任何方式可以从下载中排除依赖项(不是传递依赖项)内的特定文件。
I am switching a build from Ant + Ivy to Gradle and this was done with in Ivy before. I ask because I have a single dependency that contains many compiled wsdl jars in Artifactory that we are pulling down, but I do not want to download all of the jars in the dependency.
我正在将构建从 Ant + Ivy 切换到 Gradle,这是在 Ivy 之前完成的。我问是因为我有一个依赖项,其中包含我们正在拉下的 Artifactory 中许多已编译的 wsdl jar,但我不想下载依赖项中的所有 jar。
In Ivy it was setup like:
在常春藤中,它的设置如下:
These 6 artifacts are published to Artifactory in to one directory repo/dep.location/example/7.3/jar.
这 6 个工件发布到 Artifactory 的一个目录 repo/dep.location/example/7.3/jar 中。
<publications>
<artifact name="foo-1-0" type="jar" />
<artifact name="foo-1-0-async" type="jar" />
<artifact name="foo-1-0-xml" type="jar" />
<artifact name="bar-1-0" type="jar" />
<artifact name="bar-1-0-async" type="jar" />
<artifact name="bar-1-0-xml" type="jar" />
</publications>
This is how I retrieve only two of the six artifacts.
这就是我只检索六个工件中的两个的方式。
<dependency org="dep.location" name="example" rev="7.3"
conf="compile,runtime">
<include name="foo-1-0-async"/>
<include name="foo-1-0-xml"/>
</dependency>
Currently if I attempt to do something similar in Gradle the excludes are ignored and all six artifacts are downloaded.
目前,如果我尝试在 Gradle 中做类似的事情,排除项将被忽略,并且所有六个工件都会被下载。
compile (group:"dep.location", name:"example", version:"7.3")
{
exclude module:'foo-1-0-xml'
exclude module:'bar-1-0'
exclude module:'bar-1-0-async'
exclude module:'bar-1-0-xml'
}
I am using Gradle version 1.8.
我正在使用 Gradle 1.8 版。
回答by Raniz
I don't think Gradle has any built in support for accomplishing this, but you can clean the artifacts out from the classpath yourself.
我认为 Gradle 没有任何内置支持来完成此操作,但您可以自己从类路径中清除工件。
Inspired by this threadon the Gradle forums I came up with this:
受到Gradle 论坛上这个帖子的启发,我想出了这个:
// The artifacts we don't want, dependency as key and artifacts as values
def unwantedArtifacts = [
"dep.location:example": [ "foo-1-0-xml", "bar-1-0", "bar-1-0-async", "bar-1-0-xml"],
]
// Collect the files that should be excluded from the classpath
def excludedFiles = configurations.compile.resolvedConfiguration.resolvedArtifacts.findAll {
def moduleId = it.moduleVersion.id
def moduleString = "${moduleId.group}:${moduleId.name}:${moduleId.version}" // Construct the dependecy string
// Get the artifacts (if any) we should remove from this dependency and check if this artifact is in there
it.name in (unwantedArtifacts.find { key, value -> moduleString.startsWith key }?.value)
}*.file
// Remove the files from the classpath
sourceSets {
main {
compileClasspath -= files(excludedFiles)
}
test {
compileClasspath -= files(excludedFiles)
}
}
Note that Gradle will probably still download the files and cache them for you, but they should not be in your classpath.
请注意,Gradle 可能仍会下载文件并为您缓存它们,但它们不应位于您的类路径中。
回答by Stefan Helmerichs
I am not sure if this is what you want, but since we are using Spring Boot and Wildfly, we have to remove the tomcat-starter module from the spring boot standard package, and it looks very similar to what you've done. However, our code states:
我不确定这是否是您想要的,但由于我们使用的是 Spring Boot 和 Wildfly,我们必须从 spring boot 标准包中删除 tomcat-starter 模块,它看起来与您所做的非常相似。但是,我们的代码指出:
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
I have not checked if the corresponding jar is not downloaded or just not on the classpath, I know however that it is not used anymore.
我还没有检查相应的 jar 是否没有下载或只是不在类路径上,但我知道它不再使用了。