Gradle排除依赖项中的特定文件

bhu*_*rey 44 java dependencies artifactory gradle

我想知道是否有任何方法要排除特定文件,这些文件位于依赖项(不是传递依赖项)中,不会被下载.

我正在将一个构建从Ant + Ivy切换到Gradle,这是在Ivy之前完成的.我问,因为我有一个依赖包含Artifactory中的许多已编译的wsdl jar我们正在拉下来,但我不想下载依赖项中的所有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>
Run Code Online (Sandbox Code Playgroud)

这就是我只检索六个工件中的两个.

<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>
Run Code Online (Sandbox Code Playgroud)

目前,如果我尝试在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'
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Gradle 1.8版.

Ran*_*niz 6

我认为 Gradle 没有任何内置支持来完成此任务,但您可以自己从类路径中清除工件。

受到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)
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,Gradle 可能仍会下载文件并为您缓存它们,但它们不应该位于您的类路径中。

  • 我不确定您是否可以使用解析策略来更改已解析的工件。 (2认同)