Checkstyle 插件使用 Checkstyle 對您專案的 Java 原始碼檔案執行品質檢查,並從這些檢查產生報告。

用法

要使用 Checkstyle 插件,請在您的建置腳本中包含以下內容

build.gradle.kts
plugins {
    checkstyle
}
build.gradle
plugins {
    id 'checkstyle'
}

此插件會為專案新增許多執行品質檢查的任務。您可以透過執行 gradle check 來執行檢查。

請注意,Checkstyle 將會使用與執行 Gradle 相同的 Java 版本執行。

任務

Checkstyle 插件會將以下任務新增至專案

checkstyleMainCheckstyle

相依於: classes

對生產 Java 原始碼檔案執行 Checkstyle。

checkstyleTestCheckstyle

相依於: testClasses

對測試 Java 原始碼檔案執行 Checkstyle。

checkstyleSourceSetCheckstyle

相依於: sourceSetClasses

對給定來源集的 Java 原始碼檔案執行 Checkstyle。

新增至其他任務的相依性

Checkstyle 插件會將以下相依性新增至 Java 插件定義的任務。

check

相依於: 所有 Checkstyle 任務,包含 checkstyleMaincheckstyleTest

專案佈局

預設情況下,Checkstyle 插件預期配置檔案放置在根專案中,但這可以變更。

<root>
└── config
    └── checkstyle           (1)
        └── checkstyle.xml   (2)
        └── suppressions.xml
1 Checkstyle 配置檔案放在這裡
2 主要 Checkstyle 配置檔案

相依性管理

Checkstyle 插件會新增以下相依性配置

表 1. Checkstyle 插件 - 相依性配置
名稱 意義

checkstyle

要使用的 Checkstyle 函式庫

預設情況下,checkstyle 配置使用 com.puppycrawl.tools:checkstyle。使用的 com.puppycrawl.tools:checkstyle 版本衍生自擴展的工具版本

checkstyle {
    toolVersion = "10.12.4"
}

如果新增另一個相依性,預設的 com.puppycrawl.tools:checkstyle 相依性將會被移除

checkstyle {
    toolVersion = "10.12.4"
}

dependencies {
    checkstyle "group:artifact:version"
}

若要在 checkstyle 配置中新增相依性,同時保留對 com.puppycrawl.tools:checkstyle 的相依性,請使用以下解決方案

checkstyle {
    toolVersion = "10.12.4"
}

dependencies {
    checkstyle "com.puppycrawl.tools:checkstyle:${checkstyle.toolVersion}"
    checkstyle "group:artifact:version"
}

配置

請參閱 API 文件中的 CheckstyleExtension 類別。

內建變數

Checkstyle 插件定義了一個 config_loc 屬性,可用於 Checkstyle 配置檔案中,以定義其他配置檔案的路徑,例如 suppressions.xml

checkstyle.xml
<module name="SuppressionFilter">
    <property name="file" value="${config_loc}/suppressions.xml"/>
</module>

自訂 HTML 報告

Checkstyle 任務產生的 HTML 報告可以使用 XSLT 樣式表進行自訂,例如,可以突顯特定錯誤或變更其外觀

build.gradle.kts
tasks.withType<Checkstyle>().configureEach {
    reports {
        xml.required = false
        html.required = true
        html.stylesheet = resources.text.fromFile("config/xsl/checkstyle-custom.xsl")
    }
}
build.gradle
tasks.withType(Checkstyle) {
    reports {
        xml.required = false
        html.required = true
        html.stylesheet = resources.text.fromFile('config/xsl/checkstyle-custom.xsl')
    }
}

產生 SARIF 報告

SARIF 報告在 Checkstyle 10.3.3 和更新版本中受到支援。預設情況下未啟用。

build.gradle.kts
checkstyle {
    toolVersion = "10.3.3"
}
tasks.withType<Checkstyle>().configureEach {
    reports {
        sarif.required = true
    }
}
build.gradle
checkstyle {
    toolVersion = '10.3.3'
}
tasks.withType(Checkstyle) {
    reports {
        sarif.required = true
    }
}

變更給予 Checkstyle 的記憶體量

Checkstyle 分析在個別程序中執行。預設情況下,Checkstyle 程序會獲得 512MB 的最大堆積記憶體。在分析許多原始碼檔案時,您可能需要為此程序提供額外記憶體。您可以透過配置 Checkstyle.maxHeapSize 來變更 Checkstyle 的記憶體量。

build.gradle.kts
tasks.withType<Checkstyle>().configureEach {
    minHeapSize = "200m"
    maxHeapSize = "1g"
}
build.gradle
tasks.withType(Checkstyle) {
    minHeapSize = "200m"
    maxHeapSize = "1g"
}