直接在子项目中添加任务与从根项目添加“子项目”之间的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了直接在子项目中添加任务与从根项目添加“子项目”之间的区别相关的知识,希望对你有一定的参考价值。

我有问题,在子项目中访问配置时获取空数组

我有一个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放在了错误的地方。它们在配置阶段而不是执行阶段触发。

build phases

例如:试试

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

以上是关于直接在子项目中添加任务与从根项目添加“子项目”之间的区别的主要内容,如果未能解决你的问题,请参考以下文章

将第三方框架添加到 iOS 应用中的 Xcode 子项目或主项目

让WebStrom支持SSH协议的子项目

C#:在运行时添加项目和子项目时,VirtualMode 中的 ListView 不显示设计时列?

作为子项目添加的静态库与作为目标添加的静态库

将项目和子项目插入订单

使 SBT 子项目任务依赖于另一个子项目任务