简体   繁体   English

如何定义 settings.gradle.kts 和 build.gradle.kts 中可用的 kotlin dsl gradle 属性?

[英]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.ktssettings.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.ktsbuild.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"))
  • Gradle version 6.6.1摇篮版本 6.6.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 buildSrc/build.gradle.kts、settings.gradle.kts 和 build.gradle.kts 中导入辅助类? - How to import a helper class in buildSrc/build.gradle.kts, settings.gradle.kts, and build.gradle.kts? 有没有办法定义要在 gradle 6.0 的 settings.gradle.kts 和项目/子项目 build.gradle.kts 中使用的属性? - Is there a way to define property to be used in both settings.gradle.kts and projects/subprojects build.gradle.kts with gradle 6.0? Gradle:在 settings.gradle.kts 和 buildSrc/build.gradle.kts 之间共享存储库配置 - Gradle: share repository configuration between settings.gradle.kts and buildSrc/build.gradle.kts Gradle 6 settings.gradle.kts 属性问题 - Gradle 6 settings.gradle.kts properties problem 在 settings.gradle.kts 中读取 Gradle 属性 - Reading Gradle properties in settings.gradle.kts 为什么我无法使用 Gradle Kotlin DSL (5.0) 访问 build.gradle.kts 的“插件”部分中的项目属性? - Why I cannot access project properties in "plugins" section of build.gradle.kts with Gradle Kotlin DSL (5.0)? Kotlin DSL:将versions.gradle.kts导入另一个build.gradle.kts - Kotlin DSL: Import a versions.gradle.kts into another build.gradle.kts 使用Gradle Kotlin DSL在settings.gradle.kts中设置gradle.ext - Set gradle.ext in settings.gradle.kts with Gradle Kotlin DSL Gradle - 属性可以放在 settings.gradle.kts 中吗 - Gradle - Can properties be placed in a settings.gradle.kts Gradle 6 迁移 settings.gradle.kts - Gradle 6 migration settings.gradle.kts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM