gpt4 book ai didi

groovy - 在Gradle子项目中找不到属性

转载 作者:行者123 更新时间:2023-12-03 03:32:12 25 4
gpt4 key购买 nike

我有一个简单的gradle项目,其结构如下:

root
|
sampleapp
| |
| build.gradle
|
build.gradle
settings.gradle
gradle.properties

在我的根 build.gradle中,我有以下关闭内容:
subprojects {
ext {
flag = true
}
afterEvaluate { subproj ->
if(subproj.ext.flag) {
sampleProp = [
prop1: "abc",
prop2: "def"
]
}
}
}

运行gradle构建时,“sampleapp”项目使用sampleProp结构,但出现以下异常:
Could not find property 'sampleProp' on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@7d548fa0.

我明显在做什么错吗?

最佳答案

假设您尝试将sampleProp作为属性添加到子项目中,则可以进行以下操作:

根项目build.gradle:

subprojects {
ext {
flag = true
}

// This property will be available both at config and exec phases from subproject
if (ext.flag) {
println 'yes'
ext.configPhaseProp = [
prop1: "defined in root build.gradle",
prop2: "config"
]
}

// This property will be be available **only** in exec phase
afterEvaluate { subproj ->
if (subproj.ext.flag) {
subproj.ext.execPhaseProp = [
prop1: "defined in root build.gradle",
prop2: "exec"
]
}
}

task HelloFromRoot<<{
println "######## RootHello"
println flag
println "RootHello: $configPhaseProp.prop1 :: $configPhaseProp.prop2"
println "RootHello: $execPhaseProp.prop1 :: $execPhaseProp.prop2"
}
}

子项目build.gradle:
println '########## ConfigTime'
println flag
println "Config: $configPhaseProp.prop1 :: $configPhaseProp.prop2"
//println "Config: $execPhaseProp.prop1 :: $execPhaseProp.prop2"
//^^ Uncommenting this line will cause a "Could not find property error"

task SubHello << {
println '########## SubHello'
println flag
println "SubHello: $configPhaseProp.prop1 :: $configPhaseProp.prop2"
println "SubHello: $execPhaseProp.prop1 :: $execPhaseProp.prop2"
}

运行任务HelloFromRoot:
########## ConfigTime
true
Config: defined in root build.gradle :: config
:SubProject1:HelloFromRoot
######## RootHello
true
RootHello: defined in root build.gradle :: config
RootHello: defined in root build.gradle :: exec

运行任务SubHello:
########## ConfigTime
true
Config: defined in root build.gradle :: config
:SubProject1:SubHello
########## SubHello
true
SubHello: defined in root build.gradle :: config
SubHello: defined in root build.gradle :: exec

关于groovy - 在Gradle子项目中找不到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880968/

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