smeeb
smeeb

Reputation: 29487

Adding Gradle plugin tasks to build invocation

Here is my build.gradle:

plugins {
    id 'net.saliman.cobertura' version '2.3.1'
}

apply plugin: 'groovy'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile(
        'org.codehaus.groovy:groovy-all:2.4.6'
    )

    testCompile(
        'org.spockframework:spock-core:1.0-groovy-2.4'
    )
}

cobertura {
    coverageCheckTotalLineRate = 95

    coverageCheckHaltOnFailure = true
}

As you can see I have set up the Gradle Cobertura plugin. Typically build my application via:

./gradlew build

But to run Cobertura, I have to run a totally different invocation:

./gradlew cobertura coberturaCheck

I want to condense these so that running ./gradlew build also runs these 2 Cobertura tasks.

How can I reconfigure my build so that ./gradlew build also runs ./gradlew cobertura coberturaCheck after all tests have finished running?

Upvotes: 0

Views: 170

Answers (1)

Vampire
Vampire

Reputation: 38639

I've had a quick look at the sources of the Cobertura plugin.
The dependencies that the tests must run befor Cobertura and so on are all correctly set up by the plugin, so the right order should be taken as granted.

Actually it is the case that all tasks of type Test are set up in such a way that the Cobertura Report generation is always run after them as well as the coverage checking. Those tasks are simply disabled by default.

Adding coberturaCheck to the task graph to be executed enables all cobertura tasks including the coverage check task.

Adding cobertura to the task graph enables all cobertura tasks except of the coverage check task. So if you already added coberturaCheck to the task graph, adding cobertura has the only effect that it depends on all tasks of type Test.

As in your project the only task of type Test is the task called test which is already depended on from check which is depended on from build, it is enough to depend on coberturaCheck to enable the cobertura tasks, so just do build.dependsOn coberturaCheck in your build file, or if you like it more check.dependsOn coberturaCheck.

If you nevertheless want to depend on cobertura also, because you might fear that tasks of type Test are added in the future that are not depended on by task check, you can of course also do that. This would look like build.dependsOn tasks.cobertura, coberturaCheck or check.dependsOn tasks.cobertura, coberturaCheck.

The extra tasks. for cobertura is necessary, because there is also a project extensions called cobertura which would be taken before looking for a task called cobertura so you must explicitly state that you want the task here.

Upvotes: 1

Related Questions