【问题标题】:Lazy evaluation of a Gradle dependency definitionGradle 依赖定义的延迟评估
【发布时间】:2014-02-11 07:35:33
【问题描述】:

我正在编写一个需要使用外部库的 Gradle 插件。我希望这个库的版本可以由插件的用户从他的build.gradle 文件中定义。

我目前在构建脚本评估后定义依赖,我想知道是否有办法在插件应用时定义它,依赖信息(名称,组,版本等...)在解决依赖关系时被评估。

换句话说,是否可以使用类似于约定映射的东西来设置依赖项的字段?

【问题讨论】:

    标签: plugins gradle


    【解决方案1】:

    我认为Dependency 启用了约定映射。对于另一种方法,请参阅 Gradle 自己的代码质量插件,所有这些插件都允许在其扩展对象上设置toolVersion(例如CheckstyleExtension)。

    【讨论】:

    • 你能举个例子来说明Dependency是如何启用约定映射的吗?我的尝试失败了。
    【解决方案2】:

    在下面,您可以找到一个独立的build.gradle 文件,该文件演示了如何延迟评估依赖项版本。如果您运行./gradlew dependencies,那么您将看到消息“仅现在计算版本”。如果你运行./gradlew projects,那么你将看不到消息。

    repositories {
        jcenter()
    }
    
    configurations {
        myConfig
    }
    
    configurations.myConfig.defaultDependencies { deps ->
        // here you can perform whichever calculation is needed to be run lazily,
        // e.g., to get the version from some external file or from the extension of
        // a Gradle plugin
        println 'Calculating the version only now'
        def calculatedVersion = '4.12'
    
        // JUnit used for demo purposes only
        deps.add(project.dependencies.create("junit:junit:$calculatedVersion"))
    }
    

    defaultDependencies 方法很有趣,或者你也可以使用withDependencies

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多