摇篮排除VS包括

问题描述:

2009年,由于this question答案,以下工作:摇篮排除VS包括

task copyToLib(type: Copy) { 
    into "$buildDir/myapp/lib" 
    from configurations.runtime { 
     exclude module: 'commons-io' 
    } 
} 

我会假设,下面也应努力(包括而非排除):

task copyToLib(type: Copy) { 
    into "$buildDir/myapp/lib" 
    from configurations.runtime { 
     include module: 'commons-io' 
    } 
} 

但我m得到以下错误:

org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method include() for arguments [{module=commons-io}] on configuration ':runtime' of type org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.

这是预期还是我缺少任何明显的?

Groovy允许你省略大量的大括号和其他不必要的语法,但这也可能导致不希望的行为,就像你的情况一样。

一种常见的方法通过from(Object, Closure)方法来创建一个子CopySpec看起来就像你的代码:

[...] 
    from 'sourcePath' { 
     // configuration closure 
    } 
[...] 

首先,你传递一个对象,这将通过Project.files()进行评估,然后你通过对配置的关闭。大括号可以省略。十分简单。

但是,在你的榜样,作为对象传递的表达式是一个方法调用在ConfigurationContainer配置Configuration,就像下面的一块共同的摇篮代码:

configurations.runtime { 
    exclude module: 'xyz' 
} 

因此,通过关闭被解释为配置Configuration(全局,顺便说一句),而不是配置CopySpec。处理这个问题的方法是明确设置省略括号:

[...] 
    from(configurations.runtime, { 
     // configuration closure 
    }) 
[...] 

请注意:使用上面的例子中,你将能够同时使用exclude()include()方法,而不是你用他们的方式你的代码示例!在CopySpec中,您只能排除(或包含)文件或文件模式,而不能模块。这些方法根本不会让您通过地图,您需要通过list of stringsanother closure