将 Maven checkstyle 迁移到 gradle

Migrating maven checkstyle to gradle

我正在尝试从 maven 迁移到 gradle,checkstyle 出现奇怪的错误。

buildscript {
    repositories {
        mavenLocal()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE'
    }
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'checkstyle'


jar {
    version = '0.1.0-SNAPSHOT'
}

repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:1.1.3.RELEASE'
    }
}

dependencies {
    checkstyle 'com.puppycrawl.tools:checkstyle:6.10.1'
    compile('org.springframework.data:spring-data-commons')
    testCompile('junit:junit')
    testCompile('org.mockito:mockito-core')
    testCompile('nl.jqno.equalsverifier:equalsverifier:1.7.5')
}

test {
    maxParallelForks = 4
}

这是我遇到的错误

gradle build                                                                           slave-vi
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:checkstyleMain FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: unable to read /home/xenoterracide/IdeaProjects/entity-api/config/checkstyle/checkstyle.xml - unable to parse configuration stream - Property ${checkstyle.cache.file} has not been set

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 8.688 secs

如何才能 gradle 使用最新版本的 checkstyle?值得注意的是我的 checkstyle.xml 确实适用于 maven checkstyle 6.10.1 和 6.8

我在我的配置中发现了这个

    <property name="cacheFile" value="${checkstyle.cache.file}"/>

不记得加过,可能是sun配置文件的一部分,正好被maven插件填进去了。

这里的问题是您正在使用 checkstyle 插件,但没有为其指定任何配置。 在这里你应该添加

   checkstyle {
  ignoreFailures = true
  configFile = file("/home/jenkins/scripts/checks.xml")
   }

你应该在上面提到的位置有你的 checkstyleing 文件(你可以更改为任何你想要的)。

这是包含 checkstyle、PMD、findbugs、jacoco 等的示例 gradle 文件。 http://www.scmtechblog.net/2016/01/sample-gradle-file.html

您可以将这些信息添加到您的 build.gradle 中以供使用。