What do I want to do?
I want to tell CodeNarc which folders/directories it is supposed to scan/analyse.
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).
Results of my Research:
The only related thing I found was in the grails CodeNarc plugin documentation (https://grails.org/plugin/codenarc). There it says you can configure which source files are analysed by CodeNarc. But this seems to be the case only for the grails CodeNarc plugin not the gradle version.
The gradle CodeNarc documentation describes 3 tasks, one of them with the following:
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".
My Set-up:
My gradle project got the following structure:
.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:
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. But it seems to do that only inside the src/main/groovy and 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
sourceSets {
main {
groovy {
srcDirs = ['src/main', 'vars']
}
}
}
This way CodeNarc scans everything inside the src/main folder and everything inside the vars folder. (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:
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")
}