【发布时间】:2021-12-29 13:44:48
【问题描述】:
将 gradle 构建代码块提取到外部脚本的推荐方法是什么? 请注意,这些脚本应该支持对 gradle 项目、extra 等的引用。所以 - 在 buildSrc 中编译 kt 文件不是我想要的。
我尝试创建像logger.gradle.kts 这样的文件,这些文件似乎“知道”项目引用/编译,但是我在其中写入的任何fun 在主构建文件中都不可引用,尽管我这样应用:
apply (from = "logger.gradle.kts")
作为构建的一部分,我得到的错误是:
Unresolved reference: logInfo - 其中logInfo 是fun 中的logger.gradle.kts。
这是我正在使用的记录器文件:
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.internal.logging.services.DefaultStyledTextOutputFactory
import org.gradle.internal.logging.text.StyledTextOutput.Style
fun <R> callBasedOnContext(
ifBuildScript: KotlinBuildScript.() -> R,
ifSettingsScript: KotlinSettingsScript.() -> R
): R {
/*
* A bit of a hack to get around a compiler error when trying to do
* `is KotlinBuildScript` and `is KotlinSettingsScript`.
*/
val kotlinProjectClass = KotlinBuildScript::class
val kotlinSettingsClass = KotlinSettingsScript::class
return when {
kotlinProjectClass.isInstance(this) -> (this as KotlinBuildScript).ifBuildScript()
kotlinSettingsClass.isInstance(this) -> (this as KotlinSettingsScript).ifSettingsScript()
else -> throw AssertionError("$this is not being applied to a supported type.")
}
}
val extra: ExtraPropertiesExtension by lazy {
callBasedOnContext(
ifBuildScript = { extra },
ifSettingsScript = { (settings as ExtensionAware).extra }
)
}
fun hasPropertyHelper(propertyName: String): Boolean {
return callBasedOnContext(
ifBuildScript = { hasProperty(propertyName) },
ifSettingsScript = { (settings as ExtensionAware).extra.properties.containsKey(propertyName) }
)
}
fun propertyHelper(propertyName: String): Any? {
return callBasedOnContext(
ifBuildScript = { property(propertyName) },
ifSettingsScript = { (settings as ExtensionAware).extra.properties[propertyName] }
)
}
extra["logDebug"] = this::logDebug
extra["logInfo"] = this::logInfo
extra["logWarn"] = this::logWarn
extra["logError"] = this::logError
extra["logTitle"] = this::logTitle
extra["logStyles"] = this::logStyles
val loggerOut = DefaultStyledTextOutputFactory(null, null).create("styled_output")
val loggerOutError = loggerOut.withStyle(Style.Failure)
val loggerOutWarn = loggerOut.withStyle(Style.Description)
val loggerOutInfo = loggerOut.withStyle(Style.Success)
val loggerOutDebug = loggerOut.withStyle(Style.Normal)
val loggerOutTitle = loggerOut.withStyle(Style.Header)
fun log(message: String, out: StyledTextOutput) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
project.logger.quiet(message)
} else {
out.println(message)
}
}
fun logTitle(message: String) {
log("\n---------------------------------------------", loggerOutTitle)
log(" ${message.toUpperCase()}", loggerOutTitle)
log("---------------------------------------------", loggerOutTitle)
}
fun logDebug(message: String) {
log("[DEBUG] " + message, loggerOutDebug)
}
fun logInfo(message: String) {
log("[INFO] " + message, loggerOutInfo)
}
fun logWarn(message: String) {
log("[WARN] " + message, loggerOutWarn)
}
fun logError(message: String) {
log("[ERROR] " + message, loggerOutError)
}
fun logStyles() {
val out = DefaultStyledTextOutputFactory(null, null).create("styled_test")
log("Style: Normal", out.withStyle(Style.Normal))
log("Style: Header", out.withStyle(Style.Header))
log("Style: UserInput", out.withStyle(Style.UserInput))
log("Style: Identifier", out.withStyle(Style.Identifier))
log("Style: Description", out.withStyle(Style.Description))
log("Style: ProgressStatus", out.withStyle(Style.ProgressStatus))
log("Style: Success", out.withStyle(Style.Success))
log("Style: SuccessHeader", out.withStyle(Style.SuccessHeader))
log("Style: Failure", out.withStyle(Style.Failure))
log("Style: FailureHeader", out.withStyle(Style.FailureHeader))
log("Style: Info", out.withStyle(Style.Info))
log("Style: Error", out.withStyle(Style.Error))
}
以及 build.gradle.kts 中的用法:
plugins {
base
idea
`java-library`
scala
apply(from = "gradle_scripts/logger.gradle.kts")
}
并像这样调用:
logInfo("Application Version: ${version}")
方法失败:
Unresolved reference: logInfo
注意:如果我将函数添加到 extra(这是我在 hos 某处看到的用于公开脚本文件之间的方法的建议):
extra["logDebug"] = this::logDebug
extra["logInfo"] = this::logInfo
extra["logWarn"] = this::logWarn
extra["logError"] = this::logError
extra["logTitle"] = this::logTitle
extra["logStyles"] = this::logStyles
然后失败:
Logger_gradle@a1f3cf9 is not being applied to a supported type.
【问题讨论】:
-
请发布一个不工作的示例 Gradle 脚本以及它是如何使用的。
-
@FranciscoMateo 编辑帖子并提供更多信息
-
关注了这个回复,它似乎有效:stackoverflow.com/a/52139585/200937
标签: gradle gradle-kotlin-dsl kotlin-dsl