【问题标题】:Changing default gradle dependency file extension更改默认 gradle 依赖文件扩展名
【发布时间】:2018-05-22 22:59:27
【问题描述】:

我正在创建一个 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”),用户只需明确指定扩展名

【问题讨论】:

    标签: gradle plugins


    【解决方案1】:

    不幸的是,这似乎没有得到完全支持,理想情况下,这将按照此处描述的类似方式完成: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,而不是实际的配置名称。

    【讨论】:

    • 很遗憾,感谢提供问题链接。我不喜欢添加解决方法或技巧,所以我必须等待这个问题得到解决,或者其他解决方案可用。 (仅供参考,刚刚添加了一个comment 以便将这两个链接起来)
    【解决方案2】:

    我刚刚在我的(基于 Java 的)Gradle 插件中遇到了同样的问题,并解决了如下问题:

    myConfiguration.resolutionStrategy(strategy ->
            strategy.eachDependency(resolveDetails ->
                    resolveDetails.artifactSelection(selDetails ->
                            selDetails.selectArtifact("custom", null, null))));
    

    或者:

    myConfiguration.resolutionStrategy(strategy ->
            strategy.dependencySubstitution(substitutions ->
                    substitutions.all(depSubst ->
                            depSubst.artifactSelection(selDetails ->
                                    selDetails.selectArtifact(
                                            "custom", null, null)))));
    

    无论采用哪种方法,custom 都是 set 作为类型,然后也确定默认扩展名(因为它没有在此处明确设置)。

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      相关资源
      最近更新 更多