2

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?

Hakanai
  • 12,010
  • 10
  • 62
  • 132

1 Answers1

1

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.

Ken
  • 30,811
  • 34
  • 116
  • 155
  • Since Gradle has a lot of custom logic and things that Codenarc isn't going to catch or that will cause a lot of false positives, I would recommend using the [Gradle Lint Plugin](https://github.com/nebula-plugins/gradle-lint-plugin) instead. – jstrater Oct 12 '18 at 09:12