0
votes

I have problem that getting empty array when access configurations in subprojects

I have a gradle project (gradle version 5.3.1) with structure as below

Gradle-Test
|
|-subproject1
|    |-build.gradle
|
|-subproject2
|    |-build.gradle
|
|-build.gradle
|-settings.gradle 

In the settings.gradle

rootProject.name = 'Gradle-Test'
include 'subproject1', 'subproject2'

In the build.gradle file in subprojects, only defines the dependencies like below

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
}

Since I want to get subprojects' dependencies, I add tasks in build.gradle in root project as below

allprojects.each { p ->
    configure(p) {
        task showConfig {
            println p.name
            println p.configurations
        }
    }
}

However, when I run ./gradlew showConfig, I got

> 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

I cannot get subprojects' configurations, which return an empty array

I expected to get result like below

> 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

, which is by adding below task in each build.gradle

task showConfig {
    println project.name
    println project.configurations
}

Can anyone teach me what went wrong in my gradle?

1

1 Answers

0
votes

You have put your println's in the wrong place. They are firing in the configuration phase instead of the execution phase.

See build phases

Eg: try

task showConfig {
   doLast {
      println p.name
      println p.configurations
   }
}