Checkstyle Plugin does not add gradle tasks
Asked Answered
L

3

11

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?

Linnette answered 24/11, 2017 at 19:26 Comment(0)
M
8

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'
}
Marquesan answered 6/3, 2018 at 19:59 Comment(1)
Thomas, I should have clarified a few things: 1. I'm using android plugin com.android.[library | application] 2. I did use the --all flag. I'll update my question.Mattos
M
2

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')
    }
}
Mattos answered 27/3, 2018 at 21:12 Comment(0)
P
1

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')
}
Porterfield answered 25/11, 2017 at 17:43 Comment(1)
You need a configuration file for the whole thing to be meaningful, but you don't need it to just make the tasks appear.Marquesan

© 2022 - 2024 — McMap. All rights reserved.