gpt4 book ai didi

android - Gradle:无法将参数传递给插件中的类

转载 作者:行者123 更新时间:2023-11-30 00:20:34 25 4
gpt4 key购买 nike

我按照 example 尝试过中,并将其简化为“消息”。

class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
def extension = project.extensions.create('greeting', GreetingPluginExtension, project)
project.tasks.create('hello', Greeting) {
message = extension.message
}
}
}

class GreetingPluginExtension {
def String message = "aa"

GreetingPluginExtension(Project project) {
message = project.property(String)
}

}

class Greeting extends DefaultTask {
@Input
def String message = project.property(String)

@TaskAction
def testCoverageVerification() {
logger.quiet("Message is")
logger.quiet(message)
}
}

apply plugin: GreetingPlugin

greeting {
message = 'Hi from Gradle'
}

当我运行 ./gradlew -q hello 时,我得到了

Message is
value: null

我希望它是

Message is
value: Hi from Gradle

为什么我的问候语字符串传入失败?

最佳答案

其实很容易解释。插件(因此任务创建和变量值分配)在评估扩展 block 之前被应用和评估。因此,甚至在设置扩展的值之前就创建了任务。从您提供的链接:

The extension declaration in the build script as well as the mapping between extension properties and custom task properties occurs during Gradle's configuration phase of the build lifecycle. To avoid evaluation order issues, the actual value of a mapped property has to be resolved during the execution phase.

要使其正常工作,您可以将插件声明更改为:

class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
def extension = project.extensions.create('greeting', GreetingPluginExtension, project)
project.tasks.create('hello', Greeting) {
doFirst {
message = extension.message
}
}
}
}

但是它没有多大意义 - 所以不要使用它 ;)

关于android - Gradle:无法将参数传递给插件中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46381562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com