无法在插件块中使用项目的额外属性 [英] Can't use project extra properties in plugin block

查看:166
本文介绍了无法在插件块中使用项目的额外属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多项目构建,并且经常发现自己锁定了针对工件的所有版本.因此,在我的根项目中,我定义了类似的内容:

I have a multi-project build, and more often than not I find myself locking versions for artifacts across the board. So in my root project I define something like:

project.extra.set("pkgVersions", mapOf(
    "kotlin" to "1.2.0",
    "jooq" to "3.10.2"
))

val pkgVersions : Map<String, String> by project.extra

plugins {
    base
    kotlin("jvm") version "1.2.0" apply false
}

虽然我可以在任何地方(包括其他子项目)使用pkgVersions:

While I can use pkgVersions anywhere, including other subprojects:

val pkgVersions by rootProject.extra

jooq {
    version = pkgVersions["jooq"]
}

我无法在插件块中这样做:

I am not able to do so inside a plugin block:

plugins {
    kotlin("jvm") version pkgVersions["kotlin"]
}

给我一​​个错误在此上下文中,隐式接收器无法调用pkgVersions.如果需要,请使用显式接收器".我假设这是因为隐式接收者可能应该是文件的JVM印象?但是,它使用的是PluginDependencySpec.尝试使用this@自动完成仅显示this@plugin.这只是我的一个长期猜测.但是,关于我应该做什么的任何指示?

Gives me the error "pkgVersions can't be called in this context by implicit receiver. Use the explicit one if required". I am assuming this is because the implicit receiver should probably be the file's JVM impression? But instead it is using PluginDependencySpec. Trying an auto-complete with this@ shows only this@plugin. This is just a long-shot guess from me. But, any pointers on what I am supposed to do?

同时,有一种方法可以在gradle-kotlin-dsl中创建全局类型,例如:

Also, while we are at it, is there a way to create a global type in gradle-kotlin-dsl, for instance:

data class MyBuildType(..)

,并且无需使用buildSrc就可以在任何地方使用它吗?使用buildSrc非常简单,我不介意使用它,只是想知道.

and have it available everywhere WITHOUT using buildSrc? It's pretty straightforward with buildSrc and I don't mind using it, but just wondering.

推荐答案

build.gradle.kts

buildscript {
  val kotlinVersion by rootProject.extra { "1.3.10" }
  ...
}

build.gradle.kts 模块

dependencies {
  implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootProject.extra.get("kotlinVersion")}")
}

官方文档

这篇关于无法在插件块中使用项目的额外属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆