我正在创建一个gradle插件。在我的插件中,添加一个新配置:
project.getConfigurations().create("custom");
“自定义”配置中的所有依赖项都有扩展名“自定义”。如果我这样声明我的依赖关系:
dependencies {
custom group: 'my-custom', name: 'my-custom', version: '1.0.00'
}
它将失败,并显示以下错误消息:
Could not resolve all files for configuration ':custom'.
> Could not find my-custom:my-custom:1.0.00.
Searched in the following locations:
file:.../my-custom-1.0.00.jar
注意“.jar”扩展名。我需要显式设置扩展名,如下所示:
dependencies {
custom group: 'my-custom', name: 'my-custom', version: '1.0.00', ext: 'custom'
}
但是,如果必须始终指定扩展名,那么用户体验就不好。是否可以将我的配置的默认扩展名更改为“自定义”。这样,用户仅在扩展名与默认扩展名不同时才需要明确指定扩展名(在本例中为“.custom”)
最佳答案
不幸的是,这似乎并未得到完全支持,理想情况下,可以通过与此处所述类似的方式来完成:https://github.com/gradle/gradle/issues/1340(用configurations.all
替换configurations.custom
,并用ext
代替classifier
)
为了变通和破解,您可以创建一个闭包/方法来添加ext并添加到自定义配置中:
configurations { custom }
def cust = { project.dependencies.custom it << [ext: it.ext?:'custom'] }
dependencies {
cust group: 'my-custom', name: 'my-custom', version: '1.0.00'
cust group: 'my-custom', name: 'no-custom', version: '1.0.00', ext: 'override'
}
//note this also works outside dependencies, its not actually used
cust group: 'my-custom', name: 'oh-no-custom', version: '1.0.00'
对于一个插件,您可以将其放在项目ext中,然后将其命名为
customDependency
而不是实际的配置名称。
关于gradle - 更改默认Gradle依赖文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50477558/