gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:41:38 32 4
gpt4 key购买 nike

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

我正在将构建从 Ant + Ivy 切换到 Gradle,这之前在 Ivy 中完成。我问是因为我有一个依赖项,其中包含我们正在下拉的 Artifactory 中的许多已编译 wsdl jar,但我不想下载依赖项中的所有 jar。

在 Ivy 中,它的设置如下:

这 6 个 Artifactory 被发布到 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>

这就是我只检索六个 Artifactory 中的两个的方法。

<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 中执行类似操作,则会忽略排除项并下载所有六个 Artifactory 。

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 版。

最佳答案

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

灵感来自 this thread在 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)
}
}

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

关于java - Gradle 排除依赖项中的特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23640030/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com