Trying to exclude Tests in one task and include in Another Task, But gradle always excludes irrespective of tasks

Hi ,

I have created Two Test Tasks, I need to exclude some files in one task and include those files in another task. However, gradle is always excluding while executing the task which has included.

While Executing testSuitesInclude, tests are skipping saying that no tests found.

Refer below snippet:
Gradle Version: 5.2.1

task testSuitesInclude(type: Test) {
doFirst {
compileTestJava.include("**/*abc*")
}
filter {
useTestNG() {

        useDefaultListeners = true
        options {
            scanForTestClasses = true
            suites 'src/test/resources/test.xml'
        }
    }
    testLogging {

        events "PASSED", "FAILED", "SKIPPED"
        showStandardStreams = true

    }
}

}

task testSuitesExeclude(type: Test) {
doFirst {
compileTestJava.exclude("**/*abc*")
}
filter {
useTestNG() {

        useDefaultListeners = true
        options {
            scanForTestClasses = true
            suites 'src/test/resources/test.xml'
        }
    }
    testLogging {

        events "PASSED", "FAILED", "SKIPPED"
        showStandardStreams = true

    }
}

}

My ideal goal is to stop compiling some java files in one task and same java files has to compile in another task.

Can any one help me on this??

It seems that you don’t fully understand the Gradle lifecycle.

doFirst {
   compileTestJava.exclude("**/*abc*")
}

Here, you are “configuring” the compileTestJava task in the “execution” phase. The compileTestJava task has probably already executed by this stage so your configuration has no effect. See build phases.

I get the feeling you expect the compileTestJava task to run twice for a single build. A task will either run once or not at all for each gradle execution.

1 Like

I did a deep analysis on gradle build phases and understood that my scenario can not be achieved through tasks, it can be achieved through modules only.

Hence I have created separate module and followed it as multi project module structure. With that I was able to solve my scenario.

If you want a task to execute twice you need two instances of the task. This can either be done by adding tasks to the same project (with different configuration) or by using separate projects (each with their own task instances / configuration)

Thanks Lance, That sounds great