Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle Plugin does not add gradle tasks

i want to use checkstyle plugin in my gradle project, the gradle documentation says that it will add a few tasks: https://docs.gradle.org/current/userguide/checkstyle_plugin.html

checkstyleMain, checkstyleTest, checkstyleSourceSet

I added this into my app build.gradle file:

apply plugin: 'checkstyle'

I want to run gradle task from cmd to perform code style check, but there are no one checkstyle task. I checked the whole list by typing:

./gradlew tasks

I also tried to add checkstyle jar as library dependency to app module. Can anyone tell me what i am doing wrong and how can i get my checkstyle tasks?

like image 471
D. Snow Avatar asked Nov 24 '17 19:11

D. Snow


People also ask

How do I disable Checkstyle in Gradle?

Also you can use '-x checkstyleTest' to exclude the task via command line.


3 Answers

Well, the checkstyle plugin adds its tasks in the Other tasks virtual task group. Those are really the tasks which have not been assigned to a task group. So, they are shown only when you run ./gradlew tasks --all (note the --all).

Complete working build.gradle:

apply plugin: 'java';
apply plugin: 'checkstyle';

Then run ./gradlew tasks --all, output:

<...snip...>
Other tasks
-----------
checkstyleMain - Run Checkstyle analysis for main classes
checkstyleTest - Run Checkstyle analysis for test classes
<...snip...>

If you want the Checkstyle tasks to appear without --all, then assign them to a task group, for example:

tasks.withType(Checkstyle).each {
    it.group = 'verification'
}
like image 162
barfuin Avatar answered Oct 22 '22 15:10

barfuin


Looking at other Android projects, built with Gradle, that run checkstyle (thanks Square) I found that I needed to do some setup for the task to appear. Without the task declaration I would still see my initial error.

build.gradle:

...
apply plugin: 'checkstyle'

...

checkstyle {
    configFile rootProject.file('checkstyle.xml')
    ignoreFailures false
    showViolations true
    toolVersion = "7.8.1"
}

task Checkstyle(type: Checkstyle) {
    configFile rootProject.file('checkstyle.xml')
    source 'src/main/java'
    ignoreFailures false
    showViolations true
    include '**/*.java'
    classpath = files()
}

// adds checkstyle task to existing check task
afterEvaluate {
    if (project.tasks.getByName("check")) {
        check.dependsOn('checkstyle')
    }
}
like image 29
tir38 Avatar answered Oct 22 '22 17:10

tir38


You also need a checkstyle configuration file, either by placing one at the default location as documented or by configuring it explicitly.

For example:

checkstyle {
  config = resources.text.fromFile('config/checkstyle.xml')
}
like image 34
Louis Jacomet Avatar answered Oct 22 '22 15:10

Louis Jacomet