Gradle-codenarc-plugin

From Evgeny Goldin

(Redirected from Gradle-CodeNarc-plugin)
Jump to: navigation, search

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 so I keep using and maintaining it since it provides a full access to all properties of CodeNarc Ant task.


Usage

To use the plugin you need to add the following line to "build.gradle":


apply from: 'http://evgenyg.artifactoryonline.com/evgenyg/libs-releases-local/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 define a number of properties before the plugin is applied:


ext.codenarcVersion      = '0.19'
ext.codenarcRuleSetFiles = [ 'codenarc.groovy' ]
apply from: 'http://evgenyg.artifactoryonline.com/evgenyg/libs-releases-local/CodeNarc.gradle'


This version ensures you use the same CodeNarc version and the same ruleset file regardless of any updates to the CodeNarc project or the plugin,


'codenarc' task

  • You can run the 'gradle codenarc' with or without "-i" command line flag. When "-i" is used a summary of all options is logged.
  • When running Gradle build incrementally, the task will not run if no sources or ruleset files were modified.


# ~~~~~~~~~~~~~~~~
# Regular run
# ~~~~~~~~~~~~~~~~
 
$ gradle clean codenarc
:clean
:codenarc
 
BUILD SUCCESSFUL
 
# ~~~~~~~~~~~~~~~~
# Incremental run
# ~~~~~~~~~~~~~~~~
 
$ gradle codenarc
:codenarc UP-TO-DATE
 
BUILD SUCCESSFUL
 
# ~~~~~~~~~~~~~~~~
# Verbose run
# ~~~~~~~~~~~~~~~~
 
$ gradle -i clean codenarc
..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Generating CodeNarc report
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 RuleSet files         : [file:/Users/evgenyg/Projects/gradle-plugins/codenarc.groovy]
 Source dirs           : [/Users/evgenyg/Projects/gradle-plugins/about/src/main/groovy, /Users/evgenyg/Projects/gradle-plugins/common/src/main/groovy, ... ]
 Extensions            : [**/*.groovy]
 Report file           : [/Users/evgenyg/Projects/gradle-plugins/build/reports/codenarc.html]
 Priority 1 violations : [0]
 Priority 2 violations : [0]
 Priority 3 violations : [0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~..
[ant:codenarc] CodeNarc completed: (p1=0; p2=0; p3=0) 10441ms
 
BUILD SUCCESSFUL


CodeNarc RuleSet

"codenarc.groovy" is your local or remote ruleset file. Here's one example:


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 )
        FactoryMethodName        ( enabled : false )
        MethodName               ( enabled : false )
        SpaceAfterOpeningBrace   ( enabled : false )
        SpaceBeforeOpeningBrace  ( enabled : false )
        SpaceBeforeClosingBrace  ( enabled : false )
 
        VariableName             ( finalRegex          : /\w+/ )
        PropertyName             ( staticFinalRegex    : /\w+/ )
        AbcComplexity            ( maxMethodComplexity : 70  )
        LineLength               ( length              : 180 )
    }
}


Some more examples:


My usual strategy is to use the default ruleset file and only disable or configure a number of rules. 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 more predictable build results.


Plugin Properties

The properties below can be set in your Gradle build before the plugin is applied.


Property Name Property Type Default Value Description
ext.codenarcVersion String '0.19' CodeNarc version
ext.codenarcSources List<String> [ 'src/main/groovy', 'src/test/groovy' ] List of directories where files to be checked with CodeNarc are located
ext.codenarcExtensions List<String> [ '**/*.groovy', '**/*.gradle' ] List of Groovy file extensions for CodeNarc to check
ext.codenarcRuleSetFiles List<String> [ 'http://codenarc.sourceforge.net/StarterRuleSet-AllRulesByCategory.groovy.txt' ] List of ruleset files, path can be local or remote, starting with "http://"
ext.codenarcReportDir String 'reports' Directory where "codenarcReportFile" is created
ext.codenarcReportType String 'html' Report type, allowed values are "html", "xml", and "text"
ext.codenarcReportTitle String 'CodeNarc Report' Report title
ext.codenarcReportFile String 'codenarc.html' Report file name
ext.codenarcPriority1Violations int 0 Allowed number of Priority 1 violations.
ext.codenarcPriority2Violations int 0 Allowed number of Priority 2 violations.
ext.codenarcPriority3Violations int 0 Allowed number of Priority 3 violations.
Personal tools