【问题标题】:Checkstyle config file from dependency jar来自依赖项 jar 的 Checkstyle 配置文件
【发布时间】:2016-01-04 17:08:30
【问题描述】:

我正在尝试将 checkstyle 插件应用于我的 gradle 项目。其配置在一个单独的、共享的 jar 依赖中:

project.apply plugin: StaticAnalysisPlugin

class StaticAnalysisPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.apply plugin: 'checkstyle'     

        project.configurations {
            codingStandardsConfig
        }

        project.dependencies {
            codingStandardsConfig 'com.sample.tools:coding-standards:1.+:@jar'
        }

        def checkstyleConfigFileLocation = "classpath:sample-checkstyle-config.xml"

        project.checkstyle {
            toolVersion = '6.3'
            project.logger.debug "$project Using checkstyle version $toolVersion."
            project.logger.debug "$project Using checkstyle config from: ${checkstyleConfigFileLocation}"
            config = project.resources.text.fromFile(checkstyleConfigFileLocation)
        }

        project.checkstyleMain.source = "src/main/java"
        project.checkstyleTest.exclude "**/*"
    }
}

配置文件位于编码标准 jar 中,但我不确定如何将其连接到 checkstyle 配置中。

【问题讨论】:

    标签: gradle dependencies checkstyle


    【解决方案1】:

    我认为您应该使用configFile 选项(需要File 对象)而不是config 选项(需要带有您的实际配置的文本资源)。

    然后问题归结为确定正确的File 对象。来自this answer,看来你可以做到

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("file/test.xml").getFile());
    

    Alternative solutions 可以在 StackOverflow 上找到。确保类加载器的类路径包含您的 coding-standards.jar。您可能必须将该依赖项放在 buildscript 块中,以使其对构建脚本可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多