每当我构建 Android 应用程序时如何执行 checkstyle
How can I execute checkstyle whenever I build an Android app
我正在尝试将 checkstyle 与 Android 项目集成 - 我的 build.gradle 在下面。我基本上希望看到警告标识在构建时缺少文档的代码。使用此配置,我看到一个名为 checkstyle 的 gradle 任务,我可以手动执行它,但是当我重建项目时它不会被调用(即使我右键单击该任务并说在重建时执行')
我一定是遗漏了什么,因为其他人似乎遇到了完全相反的问题,并试图阻止它在构建时 运行。我究竟做错了什么?
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = true
}
classpath = files()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
几个月前我不得不这样做......经过大量研究和寻找。
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
// Cleaning the old log because of the creation of the new ones (not sure if totaly needed)
delete fileTree(dir: "${project.rootDir}/app/build/reports")
source 'src'
include '**/*.java'
exclude '**/gen/**'
// empty classpath
classpath = files()
//Failing the build
ignoreFailures = false
}
checkstyle {
toolVersion = '6.18'
}
project.afterEvaluate {
preBuild.dependsOn 'checkstyle'
}
这个浪费的部分很重要。 preBuild 是每次构建时都会执行的第一个任务,但它在 gradle 为 运行 之前不可见,因此您需要 .afterEvaluate。有了这个checkstyle,第一件事就是运行。在上面的代码中,您可以将 ignorefailures 设置为 true,如果检查的严重性设置为 Error,它将使构建失败,如果只有警告,则不会。
顺便说一句,这需要在模块 gradle 文件中,例如 build.gradle(Module:app)
看来我找到了自己问题的答案 - here
首先在根级别创建任务
allprojects {
repositories {
jcenter()
}
task checkstyle(type: Checkstyle) {
showViolations = true
configFile file("../settings/checkstyle.xml")
source 'src/main/java'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
// empty classpath
classpath = files()
}
}
然后在模块级别添加依赖项。这些只是附加在项目现有 build.gradle 文件的末尾。
apply plugin: 'checkstyle'
preBuild.dependsOn('checkstyle')
assemble.dependsOn('lint')
check.dependsOn('checkstyle')
我正在尝试将 checkstyle 与 Android 项目集成 - 我的 build.gradle 在下面。我基本上希望看到警告标识在构建时缺少文档的代码。使用此配置,我看到一个名为 checkstyle 的 gradle 任务,我可以手动执行它,但是当我重建项目时它不会被调用(即使我右键单击该任务并说在重建时执行')
我一定是遗漏了什么,因为其他人似乎遇到了完全相反的问题,并试图阻止它在构建时 运行。我究竟做错了什么?
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = true
}
classpath = files()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
几个月前我不得不这样做......经过大量研究和寻找。
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
// Cleaning the old log because of the creation of the new ones (not sure if totaly needed)
delete fileTree(dir: "${project.rootDir}/app/build/reports")
source 'src'
include '**/*.java'
exclude '**/gen/**'
// empty classpath
classpath = files()
//Failing the build
ignoreFailures = false
}
checkstyle {
toolVersion = '6.18'
}
project.afterEvaluate {
preBuild.dependsOn 'checkstyle'
}
这个浪费的部分很重要。 preBuild 是每次构建时都会执行的第一个任务,但它在 gradle 为 运行 之前不可见,因此您需要 .afterEvaluate。有了这个checkstyle,第一件事就是运行。在上面的代码中,您可以将 ignorefailures 设置为 true,如果检查的严重性设置为 Error,它将使构建失败,如果只有警告,则不会。
顺便说一句,这需要在模块 gradle 文件中,例如 build.gradle(Module:app)
看来我找到了自己问题的答案 - here
首先在根级别创建任务
allprojects {
repositories {
jcenter()
}
task checkstyle(type: Checkstyle) {
showViolations = true
configFile file("../settings/checkstyle.xml")
source 'src/main/java'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
// empty classpath
classpath = files()
}
}
然后在模块级别添加依赖项。这些只是附加在项目现有 build.gradle 文件的末尾。
apply plugin: 'checkstyle'
preBuild.dependsOn('checkstyle')
assemble.dependsOn('lint')
check.dependsOn('checkstyle')