Gradle-CodeNarc-plugin
From Evgeny Goldin
Gradle CodeNarc plugin allows to invoke CodeNarc Groovy code analysis through the "codenarc" task added to your Gradle build.
Contents |
Why not built-in CodeNarc plugin?
This plugin was developed before Gradle had a built-in CodeNarc Plugin to which I encourage you to switch now. This plugin will be kept alive since I'm still using it in my projects. But, please, don't confuse between the two.
Usage
To use a CodeNarc plugin add the following line to your "build.gradle":
apply from: 'https://raw.github.com/evgeny-goldin/gradle-plugins/master/src/main/groovy/CodeNarc.gradle'
No more configurations required to use the default settings. You can now run "gradle codenarc" to analyze your "*.groovy" sources in "src/main/groovy" and "src/test/groovy" folders. Report will be created in "build/reports/codenarc.html".
But using default settings can be fragile. Default property values (see the table below) will change whenever a newer CodeNarc version is released, which may cause your build to fail out of sudden. It is better if you nail down a number of settings before the plugin is applied:
ext.codenarcVersion = '0.17' ext.codenarcRuleSetFiles = [ 'codenarc.groovy' ] apply from: 'https://raw.github.com/evgeny-goldin/gradle-plugins/master/src/main/groovy/CodeNarc.gradle'
This version ensures you use the same CodeNarc version and the same ruleset file regardless of any updates to the CodeNarc project. More examples are available in GitHub and reference of all possible properties is provided below.
"codenarc" task
- You can run the
"codenarc"task with or without"-i"command line flag. With"-i"flag you'll get a summary table of major task options. - When running your build incrementally, CodeNarc will not run if no sources or ruleset files were modified.
# ~~~~~~~~~~~~~~~~ # Regular run # ~~~~~~~~~~~~~~~~ $ gradle clean codenarc :clean :codenarc log4j:WARN No appenders could be found for logger (org.codenarc.ant.CodeNarcTask). log4j:WARN Please initialize the log4j system properly. CodeNarc completed: (p1=0; p2=0; p3=1) 7237ms BUILD SUCCESSFUL Total time: 11.23 secs # ~~~~~~~~~~~~~~~~ # Incremental run # ~~~~~~~~~~~~~~~~ $ gradle codenarc :codenarc UP-TO-DATE BUILD SUCCESSFUL Total time: 2.32 secs # ~~~~~~~~~~~~~~~~ # Verbose run # ~~~~~~~~~~~~~~~~ $ gradle -i clean codenarc Starting Build .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Generating CodeNarc report ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RuleSet files : [file:/Users/evgenyg/Projects/gradle-plugins/codenarc.groovy] Source dirs : [/Users/evgenyg/Projects/gradle-plugins/src/main/groovy] Extensions : [**/*.groovy, **/*.gradle] Report file : [/Users/evgenyg/Projects/gradle-plugins/build/reports/codenarc.html] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. CodeNarc completed: (p1=0; p2=0; p3=1) 9377ms BUILD SUCCESSFUL Total time: 13.361 secs
CodeNarc RuleSet
"codenarc.groovy" is your local or remote ruleset file. Here's the file I use in a number of projects:
ruleset { description 'Gradle plugins CodeNarc RuleSet' ruleset( "http://codenarc.sourceforge.net/StarterRuleSet-AllRulesByCategory.groovy.txt" ) { DuplicateNumberLiteral ( enabled : false ) DuplicateStringLiteral ( enabled : false ) BracesForClass ( enabled : false ) BracesForMethod ( enabled : false ) BracesForIfElse ( enabled : false ) BracesForForLoop ( enabled : false ) BracesForTryCatchFinally ( enabled : false ) JavaIoPackageAccess ( enabled : false ) ThrowRuntimeException ( enabled : false ) AbcComplexity ( maxMethodComplexity : 70 ) LineLength ( length : 180 ) MethodName ( regex : /[a-z][\w\s'\(\)]*/ ) // Spock method names } }
My usual strategy is to use the default ruleset file and only disable or configure a number of rules. Of course, this makes the build vulnerable to updates of the default ruleset file by project maintainers so you may want to keep a copy of this file in your project for predictable build results.
Plugin Properties
The properties below can be set in your Gradle build before the plugin is applied. Remember to prepend them with "ext." to avoid warnings!
| Property Name | Property Type | Default Value | Description |
|---|---|---|---|
codenarcVersion
| String
| '0.17'
| CodeNarc version |
codenarcSources
| List<String>
| [ 'src/main/groovy', 'src/test/groovy' ]
| List of directories where files to be checked with CodeNarc are located |
codenarcExtensions
| List<String>
| [ '**/*.groovy', '**/*.gradle' ]
| List of Groovy file extensions for CodeNarc to check |
codenarcRuleSetFiles
| List<String>
| [ 'http://codenarc.sourceforge.net/StarterRuleSet-AllRulesByCategory.groovy.txt' ]
| List of ruleset files, path can be local or remote, starting with "http://"
|
codenarcReportDir
| String
| 'reports'
| Directory where "codenarcReportFile" is created
|
codenarcReportType
| String
| 'html'
| Report type, allowed values are "html", "xml", and "text"
|
codenarcReportTitle
| String
| 'CodeNarc Report'
| Report title |
codenarcReportFile
| String
| 'codenarc.html'
| Report file name |
codenarcPriority1Violations
| int
| 0
| Allowed number of Priority 1 violations. |
codenarcPriority2Violations
| int
| 0
| Allowed number of Priority 2 violations. |
codenarcPriority3Violations
| int
| 0
| Allowed number of Priority 3 violations. |
Examples
- Plugin usage in a Groovy project, built with Maven but using Gradle to run the plugin (ruleset file).
- Plugin usage in a Groovy scripts project (ruleset file).
- Plugin usage in a Groovy project, built with Gradle (ruleset file).
