gradle - 更改默认Gradle依赖文件扩展名

标签 gradle plugins

我正在创建一个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/

相关文章:

c# - 使用 C++/MFC 和 C# 中的插件扩展 C++/MFC 应用程序

gradle - Gradle的withType用法

java - Intellij IDEA 插件 - 未调用 PersistentStateComponent loadState

macos - 如何卸载 vim 的 Pathogen?

grails - 为什么在开发我的 grails 插件时集成测试中的依赖注入(inject)失败

java - 在 Eclipse 中分析 Java 应用程序? (插入)

gradle - 如何在Gradle中将Launch4J与混淆的jar一起使用

java - 构建项目gradle时出现错误: Connection timed out: connect ?

android - Gradle 构建失败

android - 如何为发布/调试版本提供不同的 gradle.properties 文件?