简体   繁体   English

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

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

What do I want to do? 我想做什么?

I want to tell CodeNarc which folders/directories it is supposed to scan/analyse. 我想告诉CodeNarc它应该扫描/分析哪些文件夹/目录。

I cant find anything about that on the official site ( http://codenarc.sourceforge.net/ ) or on the gradle plugin documentation ( https://docs.gradle.org/current/userguide/codenarc_plugin.html ). 我无法在官方网站( http://codenarc.sourceforge.net/ )或gradle插件文档( https://docs.gradle.org/current/userguide/codenarc_plugin.html )上找到任何相关信息。

Results of my Research: 我的研究结果:

The only related thing I found was in the grails CodeNarc plugin documentation ( https://grails.org/plugin/codenarc ). 我找到的唯一相关内容是在Grails CodeNarc插件文档( https://grails.org/plugin/codenarc )中。 There it says you can configure which source files are analysed by CodeNarc. 它说你可以配置CodeNarc分析哪些源文件。 But this seems to be the case only for the grails CodeNarc plugin not the gradle version. 但这似乎只适用于Grails CodeNarc插件而不是gradle版本。

The gradle CodeNarc documentation describes 3 tasks, one of them with the following: Gradle CodeNarc文档描述了3个任务,其中一个具有以下任务:

codenarcSourceSet — CodeNarc

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

Based on the description I thought I might be able to do something with that. 基于描述,我认为我可能能够做到这一点。 But when I look for all tasks my gradle Project got only "codenarcMain" and "codenarcTest". 但当我寻找所有任务时,我的gradle Project只获得了“codenarcMain”和“codenarcTest”。

My Set-up: 我的设置:

My gradle project got the following structure: 我的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

My build.gradle looks like that: 我的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
}

Specific goal: 具体目标:

I basically want CodeNarc to also scan scripts inside the "vars" folder/directory. 我基本上希望CodeNarc也扫描“vars”文件夹/目录中的脚本。 But it seems to do that only inside the src/main/groovy and src/test/groovy. 但它似乎只在src / main / groovy和src / test / groovy中进行。 I also don't know exactly what the default paths are because I didn't find it in the documentation. 我也不确切知道默认路径是什么,因为我没有在文档中找到它。

Seems like all you have to do is add the following to your gradle.build 好像你要做的就是将以下内容添加到你的gradle.build中

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

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

[edit] It is even possible to define your own codeNarc task: [编辑]甚至可以定义自己的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