Gradle排除依赖项中的特定文件 [英] Gradle exclude specific files inside dependency
问题描述
我想知道是否有任何方法要排除特定文件,这些文件位于依赖项(不是传递依赖项)中,不会被下载。
I was wondering if there was anyway to exclude specific files,that are inside a dependency (not a transitive dependency), from being downloaded.
我正在将一个构建从Ant + Ivy切换到Gradle,这是在Ivy之前完成的。我问,因为我有一个依赖包含Artifactory中的许多已编译的wsdl jar我们正在下拉,但我不想下载依赖项中的所有jar。
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.
在Ivy中它的设置如下:
In Ivy it was setup like:
这6个工件发布到Artifactory到一个目录repo / dep.location / example / 7.3 / jar。
These 6 artifacts are published to Artifactory in to one directory 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>
目前,如果我尝试在Gradle中执行类似的操作,则会忽略排除并下载所有六个工件。
Currently if I attempt to do something similar in Gradle the excludes are ignored and all six artifacts are downloaded.
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'
}
我正在使用Gradle版本1.8。
I am using Gradle version 1.8.
推荐答案
我不认为Gradle有任何内置支持来完成此任务,但你可以清理文物你自己从类路径出来。
I don't think Gradle has any built in support for accomplishing this, but you can clean the artifacts out from the classpath yourself.
灵感来自这个主题我想出了这个:
Inspired by this thread on the Gradle forums I came up with this:
// 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)
}
}
请注意,Gradle可能仍会下载文件并为您缓存它们,但它们不应该在您的类路径中。
Note that Gradle will probably still download the files and cache them for you, but they should not be in your classpath.
这篇关于Gradle排除依赖项中的特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!