Gradle task that changes behavior of another one

plugins

(Ievgen) #1

I’d like to copy-paste my question from here http://stackoverflow.com/q/40261454/1623597

I use gradle checkstyle plugin for static code analysis. With next configuration:

apply plugin: 'checkstyle'
checkstyle {
    toolVersion = "7.1.1"
    sourceSets = [sourceSets.main]
    ignoreFailures = true      //!!!!!!!!!!!!
    showViolations = true
    reportsDir = file("$project.buildDir/checkstyleReports")
    configFile = file("$rootDir/gradle/checkstyle.xml")
    configProperties = ['baseDir': "$project.projectDir"]
}

Checkstyle plugin adds task checkstyleMain, that checks my sources with property ignoreFailures = true it means that this task won’t fail build on any checkstyle violations.
I need new one task(copy of checkstyleMain), that will run with property ignoreFailures = false that will fail build on any violations.

My solutions should look in the next way but it doesn’t work

checkstyleMain {
    ignoreFailures = true
}
task forceCheckstyleMain(type: Checkstyle) {
    ignoreFailures = false
}

Help me please to create such task for gradle 3.1.


(Mark Vieira) #2

Rather than create another task why not just key off a property. For example.

checkstyle {
    ignoreFailures = project.hasProperty('forceCheckstyle') ? false : true
}

Then you can just run the build with the property set.

./gradlew checkstyleMain -PforceCheckstyle

(Ievgen) #3

Good idea, thanks.

How can I run checkstyleMain -PforceCheckstyle from another task? (I mean gradlew sca)

task sca (dependsOn: ['pmdMain', 'checkstyleMain', 'findbugsMain']) {
    group = "Code Quality"

    checkstyleMain.mustRunAfter pmdMain
    findbugsMain.mustRunAfter checkstyleMain
}

I created task sca that runs all static code analysis tasks.

Rather than create another task why not just key off a property.

I didn’t think that it should be too hard to run another task with overridden properties


(Stefan Oehme) #4

Different question: Why use ignoreViolations = true? :slight_smile: You can always use gradle sca --continue and the build will fail, but only after all other checks have finished too.

That way you don’t need a property or a second task at all.


(Ievgen) #5

We have Jenkins CI builds that run gradle clean build. At this moment we don’t think that checkstyle should fail this builds(usually it’s minor violations, time to rebuild all submodules, our team doesn’t decided to use checkstyle finally). Jenkins just displays violations on diagram.

To lead team members to fix checkstyle violations(locally), I created sca task. Instead of entering
gradle pmdMain checkstyleMain -PforceCheckstyle findbugsMain
they can just enter gradle sca and have profit. Recently, we started to use gitlab-ci that builds branches before merge, so I can add task sca there as well.

so I really need to have checkstyleMain that ignore violations and other task that fail build :slight_smile:

Theoretically I can set ignoreViolations = true and then add additional flag like -PignoreChekstyleViolations on jenkins for ALL CI builds, but it doesn’t look like the best solution.


(Stefan Oehme) #6

You can detect that you are on Jenkins based on environment variables and set failOnViolation to true automatically.


(Ievgen) #7

Guys, thank you for your ideas, I’ll take them into account.

but to answer on my origin question, it could be resolved in the next way:

checkstyleMain {
    gradle.taskGraph.whenReady { graph ->
        ignoreFailures = !graph.hasTask(":" + project.name + ":forceCheckstyleMain")
    }
}

task forceCheckstyleMain(dependsOn: 'checkstyleMain') {
}

It works good for my project and sca task.

One small question at the end:
Is it possible to apply --continue to the task without command line argument? (I mean to run gradlew sca instead of gradlew sca --continue)