Andrew Chen
Andrew Chen

Reputation: 335

How to specify test task run by Gradle SonarQube plugin

I am using Gradle with the sonarqube plugin and triggering it using

gradle sonarqube

However, it is calling the built in "test" task which runs all types of tests including the integration tests which do not all pass at the moment. I want to be able to specify that the sonarqube task use something like a "unitTest" task instead of the "test" task which runs everything.

Other people on the team run cucumber tests within the IDE which also uses the test task, so currently when I exclude cucumber tests in the main "test" task, they have to comment that excludes out in order for their cucumber tests to be kicked off, which is not ideal.

I am including sonarqube using the below syntax:

plugins {
    id "org.sonarqube" version "2.5"
}

How can I make the sonarqube task use another test task?

Update:

Got it to work with the below code, made subprojects run their unitTests first to generate required test report data and then run sonarqube task.

// Removes the dependency on main test task and runs unitTests for test code coverage statistics
tasks['sonarqube'].with {
    dependsOn.clear()
    subprojects.each { dependsOn("${it.name}:unitTests") }
}

dependsOn.clear() clears all dependencies, so if there were multiple, I would probably have to do something like

taskName.enabled=false

and then re-enable it after the task was done unless there is a easier way to do it? I've tried dependsOn.remove("test") but that didn't work.

Upvotes: 7

Views: 8259

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14493

The sonarqube tasks simply depends on the test task by default, according to the docs:

To meet these needs, the plugins adds a task dependency from sonarqube on test if the java plugin is applied.

You can easily remove this task dependency and add your own ones:

task unitTest {
    doLast {
        println "I'm a unit test"
    }
}

tasks['sonarqube'].with {
    dependsOn.clear()
    dependsOn unitTest
}

Now, only the unitTest task (and its dependencies) will be executed whenever you execute the sonarqube task. Please note, that dependsOn is just a list of dependency entries. You can modify it in many different ways.

Upvotes: 10

Related Questions