How do I get the Gradle CodeNarc task to check the Gradle build files?

627 Views Asked by At

Gradle build files (*.gradle) are Groovy, so I'd like to perform code style checks on the build files, as with any other code in the project. So I figured CodeNarc would work for this.

The minimum to get CodeNarc itself hooked into Gradle appears to be the following:

apply plugin: 'groovy'
apply plugin: 'codenarc'

dependencies {
  codenarc 'org.codenarc:CodeNarc:1.1'
}

tasks.withType(CodeNarc) {
  // contents of the config file are not interesting enough to list
  configFile = rootProject.file('config/codenarc/codenarc.groovy')
}

When I run this, it does nothing, because we don't have any groovy main or test files. That's fine, we never intend to use Groovy in the project itself.

But what I'm wondering is, how we can point this thing at the Gradle build files themselves?

1

There are 1 best solutions below

1
Ken On

As you've perhaps seen, there's some advice from 2014 here https://discuss.gradle.org/t/codenarc-gradle-files-themselves/4108

So the best I could do myself so far was to write a shell-script for linux, like this

#! /usr/bin/env sh
set -e -x
export GROOVY=/path/to/groovy-all-2.3.3.jar
export CODENARC=/path/to/CodeNarc-0.21.jar
export LOG4J=/path/to/log4j-1.2.17.jar
export CONFIG=/path/to/ruleseDir
java -cp "$LOG4J:$GROOVY:$CONFIG:$CODENARC" \
    org.codenarc.CodeNarc -includes=**/*.gradle -basedir=. -rul

Things might have changed since.