Search code examples
gradlegroovycodenarc

Configure additional filename extensions for Gradle CodeNarc plugin


I am currently trying to get the CodeNarc plugin for Gradle 8.0.2 to work in my project.

Due to some reasons, the Groovy files have different extensions. At the moment, the plugin only seems to run for the *.groovy files.

Let's say I have a build.gradle file like this:

plugins {
    id 'groovy'
    id 'java'
    id 'codenarc'
}

repository {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.groovy:groovy-all:4.0.10'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['directory1', 'directory2']
        }
    }
}

Now I have the following files:

  • directory1/utils.groovy
  • directory1/something.groovy
  • directory2/another.myextension

Running ./gradlew codenarcMain --info will not print:

No matching files found for FileSet with basedir [/home/path/to/project/directory2]

Adding

codenarcMain {
    include('**/*.myextension', '**/*.groovy')
}

does not seem to change anything.

What is the correct approach to register custom extensions for the CodeNarc plugin from within Gradle?


Solution

  • I wasn't able to get gradle's in-built codenarc plugin recognize an extension other than .groovy even after configuring the groovy compiler to recognize the new extension. However, following an example in the codenarc website, I was able to achieve this by creating a custom codenarc gradle task based on the ant codenarc task. This task is configured to process specific source folder(s)(src/test/groovy in the example) and files with specific extensions(.groovy and .dummy in the example).

    configurations {
        codenarc
    }
    
    dependencies {
        String groovyVersion = '3.0.18'
    
        codenarc group: 'org.codehaus.groovy', name: 'groovy-templates', version: groovyVersion
        codenarc group: 'org.codehaus.groovy', name: 'groovy-xml', version: groovyVersion
        codenarc group: 'org.slf4j', name: 'slf4j-api', version: '1.7.35'
        codenarc group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.12'
        codenarc group: 'org.codenarc', name: 'CodeNarc', version: '3.2.0'
    }
    
    tasks.register('codenarcTest') {
        String reportsFile = 'build/reports/codenarc.html'
        inputs.files(fileTree('src/test/groovy'))
        outputs.file("$projectDir/$reportsFile")
    
        doLast {
            ant.taskdef(name:'codenarc', 
                        classname:'org.codenarc.ant.CodeNarcTask', 
                        classpath: configurations.codenarc.asPath)
            ant.codenarc(
               ruleSetFiles: 'rulesets/imports.xml,rulesets/unused.xml',
               maxPriority1Violations: 0, maxPriority2Violations: 0,
               maxPriority3Violations: 0,
            ) {
               fileset(dir:'src/test/groovy') {
                   include(name: '**/*.groovy')
                   include(name: '**/*.dummy')
               }
    
               report(type:'html') {
                   option(name: 'outputFile', value: reportsFile)
               }
            }
        }
    }