Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for a gradle subproject task depending on a parent project's task?

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.

like image 482
Eddified Avatar asked Nov 17 '16 22:11

Eddified


People also ask

What is subproject in Gradle?

In a multi-project gradle build, you have a rootProject and the subprojects. The combination of both is allprojects. The rootProject is where the build is starting from. A common pattern is a rootProject has no code and the subprojects are java projects.

Which is the valid syntax to set default tasks in Gradle?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.

What is rootDir in Gradle?

rootDir. The root directory of this project. The root directory is the project directory of the root project.

How do I define a task in Gradle?

Each project is made up of different tasks and a task is a piece of work which a build performs. The task might be compiling some classes, storing class files into separate target folder, creating JAR, generating Javadoc, or publishing some achieves to the repositories.


1 Answers

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

runDifferentTests.dependsOn rootProject.bTask
like image 185
Marwin Avatar answered Sep 23 '22 09:09

Marwin