To check code written in the Groovy language, we can use CodeNarc. CodeNarc has several rules to do a static analysis of Groovy code. Gradle has a CodeNarc plugin, so we can apply the rules from CodeNarc to our Groovy code base. If we apply the plugin, we automatically get a
codenarcMain
and codenarcTest
target. Also, for each custom source set, we get a new task named codenarc<SourceSet>
. All these tasks are dependency tasks of the check
task.
The CodeNarc library is not included with Gradle. We need to define a repository in our build file that contains CodeNarc. If we invoke a codenarc
task, then Gradle sets CodeNarc dependencies. We can change the version of CodeNarc that we want to use by setting the
codenarc.toolVersion
property.
The plugin defines that we provide a CodeNarc configuration file with the name codenarc.xml
in the directory config/codenarc
. We can change the reference to the configuration file with the
configFile
property of the plugin.
Let's create the following example build file and apply the CodeNarc plugin for a Groovy project. We will change the version of CodeNarc that we want to use. We will also redefine the location of the CodeNarc configuration file to config/codenarc/custom.xml
:
apply plugin: 'groovy' apply plugin: 'codenarc' // Repository definition to get CodeNarc libraries. repositories { mavenCentral() } codenarc { // Change version of CodeNarc. toolVersion = 0.17 // Change name of configuration file. Default value // is file('config/codenarc/codenarc.xml') configFile = file('config/codenarc/rules.groovy') }
When we run the check
task and our Groovy code base starts violating the configured CodeNarc rules, the build will fail. If we don't want the build to fail on a violation, we can set the property ignoreFailures
to true
. We can set this for all codenarc
tasks with the codenarc.ignoreFailures
property. We can also set this property for individual codenarc
tasks.
The following build file shows that we set the property ignoreFailures
for all the codenarc
tasks:
apply plugin: 'groovy'
apply plugin: 'codenarc'
repositories {
mavenCentral()
}
codenarc.ignoreFailures = true
The codenarc
tasks create an HTML report with the found results, and place it in the build/reports/codenarc
directory. The name of the file is defined by the source set name for which the task is executed. We can also choose different output formats. We can set the output to XML or text file formats. We can change the format of the reports with the
reports()
method of the codenarc
tasks. To change the output directory, we can set the property codenarc.reportsDir
in our project:
apply plugin: 'groovy' apply plugin: 'codenarc' // Repository definition to get CodeNarc libraries. repositories { mavenCentral() } codenarc { toolVersion = 0.17 configFile = file('config/codenarc/rules.groovy') // Change output directory for reports. reportsDir = file("${reporting.baseDir}/codenarc-output") } tasks.withType(CodeNarc) { task -> reports { // Enable text format. text.enabled = true // Configure XML output. xml { enabled = true // Change destination file. destination = file("${codenarc.reportsDir}/${task.name}.xml") } } }
18.117.187.113