直接在子项目中添加任务和通过根项目中的“子项目”添加任务之间的区别

时间:2019-04-09 05:59:13

标签: 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'
}

由于我想获得子项目的依赖关系,因此我在根项目的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
}

有人能教我我的毕业证书出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

您将println放在错误的位置。它们在配置阶段而不是执行阶段触发。

请参见build phases

例如:尝试

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