2014-05-13 106 views
39

我想知道是否有排除依赖项(而不是传递依赖项)内的特定文件被下载的问题。Gradle排除依赖关系中的特定文件

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

在常春藤它是设置这样的:

这6个构件发布到artifactory的一个目录回购/ dep.location /示例/ 7.3 /罐。

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

这就是我如何检索六个工件中的两个。

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

目前,如果我试图做摇篮类似的东西排除被忽略,所有六个文物被下载。

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。

+0

你试图排除的东西是工件,而不是模块。在我意识到的Gradle中包含/排除工件的唯一受支持的方式是按类型,这在这里没有帮助。 –

+0

这在gradle中不受支持。但另一种选择是改变你如何发布到artifactory。 为什么不开始在artifactory中发布个别的jar文件? – vkg

+0

好的,谢谢。 – bhumphrey

回答

3

我不认为Gradle有任何内置的支持来完成这个任务,但是您可以自己从classpath中清除工件。

通过this thread在摇篮论坛的启发我想出了这一点:

// 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) 
    } 
} 

注意摇篮仍然可能会下载的文件和缓存他们为你,但他们不应该在你的classpath。

+0

Gradle已经建立了支持。它是'分辨率策略' – smilyface

+0

我不确定您可以使用分辨率策略来更改哪些手工制品已解决。 – Raniz

1

我不确定这是否是你想要的,但由于我们使用的是Spring Boot和Wildfly,我们必须从spring引导标准包中删除tomcat-starter模块,它看起来与你的'已经完成。然而,我们要求写明:

configurations { 
    compile.exclude module: "spring-boot-starter-tomcat" 
} 

如果相应的罐子没有下载或只是没有在classpath我没有检查,但我知道它已不再使用。