【问题标题】:Gradle translate groovy to kotlin for pluginsGradle 将 groovy 转换为 kotlin 以获取插件
【发布时间】:2020-07-22 12:05:28
【问题描述】:

我目前有一个运行良好的 init.gradle 文件:

initscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("com.github.ben-manes:gradle-versions-plugin:+")
    }
}
allprojects {

    apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
}

现在我想转向 kotlin DSL,因为我正在处理的所有其他项目都已迁移。

所以我创建了这个:

initscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath("com.github.ben-manes:gradle-versions-plugin:+")
  }
}
allprojects {
  apply(plugin = com.github.benmanes.gradle.versions.VersionsPlugin)
}

不幸的是,我收到以下错误:

Script compilation error:

  Line 11:   apply(plugin = com.github.benmanes.gradle.versions.VersionsPlugin)
                                                                ^ Classifier 'VersionsPlugin' does not have a companion object, and thus must be initialized here

当我尝试将" 放在插件周围时,我收到错误:

* What went wrong:
Plugin with id 'com.github.benmanes.gradle.versions.VersionsPlugin' not found.

当我尝试时

apply<VersionsPlugin>()

我明白了:

  Line 10: apply<VersionsPlugin>()
                 ^ Unresolved reference: VersionsPlugin

当我尝试时:

apply(plugin = "com.github.ben-manes.versions")

我明白了:

* What went wrong:
Plugin with id 'com.github.ben-manes.versions' not found.

gradle documentation 中,我刚刚找到了有关如何初始化新插件的提示,而不是如何添加现有插件。

这里有人可以帮助我吗?

【问题讨论】:

    标签: kotlin gradle plugins


    【解决方案1】:

    为此有一个特殊的 Kotlin 扩展函数

    apply<VersionsPlugin>()
    
    // or
    apply(plugin = "com.github.ben-manes.versions")
    

    更新: 第一个选项对我来说效果很好。 init.gradle.kts文件的完整内容:

    import com.github.benmanes.gradle.versions.VersionsPlugin
    
    initscript {
        repositories {
            jcenter()
        }
    
        dependencies {
            classpath("com.github.ben-manes:gradle-versions-plugin:+")
        }
    }
    
    allprojects {
        apply<VersionsPlugin>()
    }
    

    用法:./gradlew dependencyUpdates -I init.gradle.kts

    第二个选项确实失败了,抱歉。原因我不清楚。

    【讨论】:

    • 不幸的是,对于第一个建议,我得到了这个:Line 11: apply&lt;VersionsPlugin&gt;() ^ Unresolved reference: VersionsPlugin 而对于你的第二个建议,我得到了这个:* What went wrong: Plugin with id 'com.github.ben-manes.versions' not found.
    • 你试过加import com.github.benmanes.gradle.versions.VersionsPlugin吗?
    • 非常感谢。我在allProjects 范围之外尝试了apply&lt;VersionsPlugin&gt;(),因为我在“正常”构建脚本中已经习惯了。但是把它放在里面就可以了,非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多