繁体   English   中英

如何定义CodeNarc Gradle插件将扫描/分析的文件夹/目录

[英]How to define which Folders/Directories the CodeNarc Gradle Plugin will scan/analyse

我想做什么?

我想告诉CodeNarc它应该扫描/分析哪些文件夹/目录。

我无法在官方网站( http://codenarc.sourceforge.net/ )或gradle插件文档( https://docs.gradle.org/current/userguide/codenarc_plugin.html )上找到任何相关信息。

我的研究结果:

我找到的唯一相关内容是在Grails CodeNarc插件文档( https://grails.org/plugin/codenarc )中。 它说你可以配置CodeNarc分析哪些源文件。 但这似乎只适用于Grails CodeNarc插件而不是gradle版本。

Gradle CodeNarc文档描述了3个任务,其中一个具有以下任务:

codenarcSourceSet — CodeNarc

    Runs CodeNarc against the given source set’s Java source files

基于描述,我认为我可能能够做到这一点。 但当我寻找所有任务时,我的gradle Project只获得了“codenarcMain”和“codenarcTest”。

我的设置:

我的gradle项目得到以下结构:

.git
    -> ...
.gradle
    -> ...
config
    -> codenarc -> MyRuleset.groovy
gradle
    -> ...
src
    -> main
            -> groovy -> ...
            -> ressources
    -> test
            -> groovy -> ...
            -> ressources
vars
    -> ...
.gitignore
build.gradle
gradlew
gradlew.bat
ReadMe.txt
settings.gradle

我的build.gradle看起来像这样:

plugins {
    // Apply the groovy plugin to add support for Groovy
    id 'groovy'
    id 'codenarc'
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    maven { url "http://repo.jenkins-ci.org/releases/" }
    maven { url "http://central.maven.org/maven2/" }
}

dependencies {
    // https://mvnrepository.com/artifact/org.jenkins-ci.main/jenkins-core
    compile group: 'org.jenkins-ci.main', name: 'jenkins-core', version: '2.179'

    // https://mvnrepository.com/artifact/org.jenkins-ci.plugins/cloudbees-folder
    compile 'org.jenkins-ci.plugins:cloudbees-folder:5.3@jar'

    // https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-api
    compile 'org.jenkins-ci.plugins.workflow:workflow-api:2.35@jar'

    // https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-job
    compile 'org.jenkins-ci.plugins.workflow:workflow-job:2.32@jar'

    // https://mvnrepository.com/artifact/com.cloudbees/groovy-cps
    compile group: 'com.cloudbees', name: 'groovy-cps', version: '1.28'


    // https://mvnrepository.com/artifact/org.codenarc/CodeNarc
    codenarc group: 'org.codenarc', name: 'CodeNarc', version: '1.1'

    // Use the latest Groovy version for building this library
    implementation 'org.codehaus.groovy:groovy-all:2.5.6'

    // Use the awesome Spock testing and specification framework
    testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
}

codenarc {
    configFile = file("${project.projectDir}/config/codenarc/MyRuleset.groovy")
    reportFormat = 'html'
    reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
    maxPriority1Violations = 0
    maxPriority2Violations = 0
    maxPriority3Violations = 0
}

具体目标:

我基本上希望CodeNarc也扫描“vars”文件夹/目录中的脚本。 但它似乎只在src / main / groovy和src / test / groovy中进行。 我也不确切知道默认路径是什么,因为我没有在文档中找到它。

好像你要做的就是将以下内容添加到你的gradle.build中

sourceSets {
    main {
        groovy {
        srcDirs = ['src/main', 'vars']
        }
    }   
}

这样CodeNarc扫描src / main文件夹中的所有内容以及vars文件夹中的所有内容。 https://discuss.gradle.org/t/gradle-codenarc-plugin-how-to-change-target-folder-for-the-scan/32102/2

[编辑]甚至可以定义自己的codeNarc任务:

sourceSets {
    main {
        groovy {
            srcDirs = ['src']
        }
        resources {
            srcDirs = []
        }
    }
    test {
        groovy {
            srcDirs = ['test']
        }
        resources {
            srcDirs = []
        }
    }
    vars {
        compileClasspath += main.compileClasspath
        compileClasspath += main.output
        groovy {
            srcDirs = ['vars']
        }
        resources {
            srcDirs = []
        }
    }
}

codenarc {
    toolVersion = '1.3'
    configFile = file("${project.projectDir}/config/codenarc/ruleset.groovy")
    sourceSets = [sourceSets.main, sourceSets.test, sourceSets.vars]
    reportsDir = file("$buildDir/reports/StaticCodeAnalysis")
}

codenarcMain {
    configFile = file("${project.projectDir}/config/codenarc/MainRuleset.groovy")
}

codenarcTest {
    configFile = file("${project.projectDir}/config/codenarc/TestRuleset.groovy")
}
codenarcVars {
    configFile = file("${project.projectDir}/config/codenarc/VarsRuleset.groovy")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM