What is the syntax for a gradle subproject task de

2020-06-16 05:04发布

问题:

I need some help with a Gradle build file. I have some subprojects which depend on tasks in the top folder. I just need to take a test task and split it into two separate test tasks. Currently the file system structure looks like (tab indicates files inside a directory):

top-level-project-folder
    A.gradle
    B.gradle
    C-subproject-folder
        C.gradle
    D-subproject-folder
        D.gradle

Contents of A.gradle (before refactor):

subprojects {
    tasks.test.dependsOn {
        bTask
    }
}
apply from: 'B.gradle'

Contents of C.gradle (before refactor):

test {
    ...
}

After the refactor, C.gradle needs to look like:

test {
    ...
}

task runDifferentTests(type : Test) {
    ...
}

The tricky part is that C.gradle's test task currently depends on bTask. However, after the refactor, C.gradle's test task should not depend on bTask, but the new runDifferentTests task should depend on bTask. (Currently, D.gradle's test task is marked as depending on bTask, but it does not actually depend on it -- I'd like to remove that dependency. The only task in the two subprojects which depends on bTask is the new runDifferentTests task.)

I've tried some different things but can't seem to find a working solution.

回答1:

Just remove the declaration in subprojects and declare your dependency directly in the subproject, in C.gradle:

runDifferentTests.dependsOn rootProject.bTask


回答2:

There are a few syntax solutions here:

runDifferentTests.dependsOn (":bTask")

runDifferentTests.dependsOn rootProject.bTask

task runDifferentTests(type : Test, dependsOn: [":bTask"]) {
...
}

task runDifferentTests(type : Test) {
dependsOn rootProject.bTask
...
}

//in the root build.gradle to apply to all subprojects at once.
subprojects {
    runDifferentTests.dependsOn (":bTask")
}

runDifferentTests.dependsOn {
    tasks.findAll { task -> task.name.startsWith('bTask') }
}

: can be used to go a level up instead of rootProject depends on the preference and the project structure