我有问题,在子项目中访问配置时获取空数组
我有一个gradle项目(gradle版本5.3.1),结构如下
Gradle-Test
|
|-subproject1
| |-build.gradle
|
|-subproject2
| |-build.gradle
|
|-build.gradle
|-settings.gradle
在settings.gradle中
rootProject.name = 'Gradle-Test'
include 'subproject1', 'subproject2'
在子项目的build.gradle文件中,仅定义依赖项,如下所示
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
}
由于我想获得子项目的依赖项,我在root项目的build.gradle中添加任务,如下所示
allprojects.each { p ->
configure(p) {
task showConfig {
println p.name
println p.configurations
}
}
}
然而,当我运行./gradlew showConfig
时,我得到了
> Configure project :
Gradle-Test
[configuration ':annotationProcessor', configuration ':apiElements', configuration ':archives', configuration ':compile', configuration ':compileClasspath', configuration ':compileOnly', configuration ':default', configuration ':implementation', configuration ':runtime', configuration ':runtimeClasspath', configuration ':runtimeElements', configuration ':runtimeOnly', configuration ':testAnnotationProcessor', configuration ':testCompile', configuration ':testCompileClasspath', configuration ':testCompileOnly', configuration ':testImplementation', configuration ':testRuntime', configuration ':testRuntimeClasspath', configuration ':testRuntimeOnly']
subproject1
[]
subproject2
[]
BUILD SUCCESSFUL in 0s
我无法获得子项目的配置,它返回一个空数组
我希望得到如下结果
> Configure project :
Gradle-Test
[configuration ':annotationProcessor', configuration ':apiElements', configuration ':archives', configuration ':compile', configuration ':compileClasspath', configuration ':compileOnly', configuration ':default', configuration ':implementation', configuration ':runtime', configuration ':runtimeClasspath', configuration ':runtimeElements', configuration ':runtimeOnly', configuration ':testAnnotationProcessor', configuration ':testCompile', configuration ':testCompileClasspath', configuration ':testCompileOnly', configuration ':testImplementation', configuration ':testRuntime', configuration ':testRuntimeClasspath', configuration ':testRuntimeOnly']
> Configure project :subproject1
subproject1
[configuration ':subproject1:annotationProcessor', configuration ':subproject1:apiElements', configuration ':subproject1:archives', configuration ':subproject1:compile', configuration ':subproject1:compileClasspath', configuration ':subproject1:compileOnly', configuration ':subproject1:default', configuration ':subproject1:implementation', configuration ':subproject1:runtime', configuration ':subproject1:runtimeClasspath', configuration ':subproject1:runtimeElements', configuration ':subproject1:runtimeOnly', configuration ':subproject1:testAnnotationProcessor', configuration ':subproject1:testCompile', configuration ':subproject1:testCompileClasspath', configuration ':subproject1:testCompileOnly', configuration ':subproject1:testImplementation', configuration ':subproject1:testRuntime', configuration ':subproject1:testRuntimeClasspath', configuration ':subproject1:testRuntimeOnly']
> Configure project :subproject2
subproject2
[configuration ':subproject2:annotationProcessor', configuration ':subproject2:apiElements', configuration ':subproject2:archives', configuration ':subproject2:compile', configuration ':subproject2:compileClasspath', configuration ':subproject2:compileOnly', configuration ':subproject2:default', configuration ':subproject2:implementation', configuration ':subproject2:runtime', configuration ':subproject2:runtimeClasspath', configuration ':subproject2:runtimeElements', configuration ':subproject2:runtimeOnly', configuration ':subproject2:testAnnotationProcessor', configuration ':subproject2:testCompile', configuration ':subproject2:testCompileClasspath', configuration ':subproject2:testCompileOnly', configuration ':subproject2:testImplementation', configuration ':subproject2:testRuntime', configuration ':subproject2:testRuntimeClasspath', configuration ':subproject2:testRuntimeOnly']
BUILD SUCCESSFUL in 0s
,通过在每个build.gradle中添加以下任务
task showConfig {
println project.name
println project.configurations
}
谁能告诉我在我的gradle中出了什么问题?
你把你的println放在了错误的地方。它们在配置阶段而不是执行阶段触发。
例如:试试
task showConfig {
doLast {
println p.name
println p.configurations
}
}