beforeFoo
。
不感到惊讶 - 必须在
beforeFoo
之前进行,我有这样做:
foo
我开始觉得我的
task foo << {
dependsOn 'beforeFoo'
}
应该得到一些信息 对于
beforeFoo
而言,这是可见的,不是foo
,我需要以某种方式通过它。在分离的.gradle文件中定义的任务,这些文件由rootbuild.gradle
通过
beforeFoo
expression.
我已经尝试了什么:1
apply from
foo
在不同的.gradle文件中定义,而非文件是根,因此不可能重复使用project.ext.tasks之间的我信息
2
要使“ fradfoo'”一个延伸的类,然后创建类似的东西
beforeFoo
没有,如果在当前的.gradle file中定义某个地方,则在未见之前不可见。
不能相信我不能轻易地为DefaultTask
表达指定争论。如果得到帮助,我会很高兴。
我可能不了解这个问题,但是给出了::
task `beforeFoo` (type: BeforeFooClass){
myInfo='info'
}
和
a.dependsOn b
:build.gradle
和
apply from: 'before.foo.gradle'
apply from: 'foo.gradle'
(带有“配置”阶段和“执行”阶段的单独代码):before.foo.gradle
抓住此结果:
task beforeFoo() << {
println "executing beforeFoo"
println "value is: " + project.ext.value
}
是:
foo.gradle
在配置阶段设置值
task foo(dependsOn: "beforeFoo") {
println "configuring foo"
project.ext.value = "is set by foo"
}
foo << {
println "executing foo"
}
bash$ gradle foo
configuring foo
:beforeFoo
executing beforeFoo
value is: is set by foo
:foo
executing foo
在执行阶段打印文本
llet假设您有以下项目:
foo
foo
当您尝试执行任务时,您将获得输出:
apply from: 'beforeFoo.gradle'
apply from: 'foo.gradle'
对于这种情况,您有两个选择:
使用Groovy的Lazygstring:只需从foo中添加the the the the the the the the the the the。
task beforeFoo {
ext.someValue = foo.someValue
doLast {
println someValue
}
}
查看Groovy Document的GSTRINGS中的分布
使用task foo {
dependsOn 'beforeFoo'
ext.someValue = "I'm value from foo"
}
block inforefoo.gradle:
foo
(闭合)
添加了一个封闭式 该项目已被评估。该项目传递给关闭 作为参数。构建文件时,这样的听众会通知 属于该项目已执行。父母可能会 示例将这样的听众添加到其儿童项目中。这样的听众可以 进一步根据孩子的状态配置这些儿童项目 运行构建文件后的项目。 Gradle的ProjectAPI
结果是:
$ gradle foo
FAILURE: Build failed with an exception.
* Where:
Script 'beforeFoo.gradle' line: 2
* What went wrong:
A problem occurred evaluating script.
> Could not get unknown property 'foo' for task ':beforeFoo' of type org.gradle.api.DefaultTask.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.488 secs
"{ ->
thefore.gradle
}"
gradle.properties
ext.someValue = "${ -> foo.someValue }"
i更新了@michael Easter的代码,用于Gradle 8.9,2025年:
afterEvaluate
我得到的结果略有不同,这仍然使我感到惊讶:afterEvaluate { task beforeFoo { ext.someValue = foo.someValue doLast { println someValue } } }