[英]How to define kotlin dsl gradle properties available in settings.gradle.kts and build.gradle.kts?
I have a multiplatform (kotlin) multiproject gradle project written using the kotlin DSL.我有一个使用 kotlin DSL 编写的多平台(kotlin)多项目 gradle 项目。 Bear with me.
忍受我。 In settings.gradle.kts, I have two major types of include helpers to deal with alternative project layout, a regular module (module/src) and a mutliplatform module (module/js, module/common, module/jvm).
在 settings.gradle.kts 中,我有两种主要类型的包含帮助程序来处理替代项目布局,一个常规模块(module/src)和一个多平台模块(module/js、module/common、module/jvm)。 I then create variables to hold lists of each which I use with these helpers to include everything the way I'd like.
然后我创建变量来保存我与这些助手一起使用的每个列表,以按照我想要的方式包含所有内容。
I'd like to be able to reuse this information defined in settings (the lists) in my build.gradle.kts file.我希望能够重用在我的 build.gradle.kts 文件的设置(列表)中定义的这些信息。
Every time I try to do this the way I would in the build file, I get the same error:每次我尝试按照在构建文件中的方式执行此操作时,都会遇到相同的错误:
val test: List<String> by rootProject.extra(listOf("game", "spine"))
or或者
val test: List<String> by extra(listOf("game", "spine"))
The error I get is:我得到的错误是:
Line 68: val test: List<String> by rootProject.extra(listOf("game", "spine"))
^ Expression 'extra' cannot be invoked as a function. The function 'invoke()' is not found
My guess is that settings is not extension aware, so even though this
is of type Project (bc it is the root project) in settings, it doesn't matter and you can't use extra.我的猜测是设置不知道扩展名,所以即使
this
是设置中的项目类型(bc 它是根项目),也没关系,你不能使用额外的。 The only way I can think of to do this would be to define project properties back in a gradle properties file, but I'm not very keen on that given this doesn't seem like the type of information to keep there.我能想到的唯一方法是在 gradle 属性文件中定义项目属性,但我不是很热衷于此,因为这似乎不是要保留在那里的信息类型。
Is there anything that allows for an extension property to be lazily initialized when buildscript is underway?是否有任何允许在 buildscript 进行时延迟初始化扩展属性的内容?
I realize this is a bit old of a post, but I recently gave an answer here .我意识到这是一个有点旧的帖子,但我最近在这里给出了答案。 But will repeat it since I came across this question also....
但是会重复一遍,因为我也遇到了这个问题......
In settings.gradle.kts在settings.gradle.kts
// // This works in settings.gradle
// gradle.ext.GLOBAL_VAR = "This is a global value"
// println("settings.gradle ::: " + gradle.GLOBAL_VAR)
// This works in settings.gradle.kts
val settingsValue = "This value was set in settings.gradle.kts"
if (gradle is ExtensionAware) {
(gradle as ExtensionAware).extra["GLOBAL_VAR"]=settingsValue
println("settings.gradle.kts ::: " + (gradle as ExtensionAware).extra.get("GLOBAL_VAR"))
}
in build.gradle.kts在build.gradle.kts
// // This works in settings.gradle
// println("build.gradle ::: " + gradle.GLOBAL_VAR)
// This works in settings.gradle.kts
if (gradle is ExtensionAware) println("build.gradle.kts ::: " + (gradle as ExtensionAware).extra.get("GLOBAL_VAR"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.