Gradle CodeNarc Plugin - How to change target folder for the scan

Hello,
I am using the Gradle CodeNarc Plugin https://docs.gradle.org/current/userguide/codenarc_plugin.html.
With this I am able to scan/check my groovy files as expected for specific rules.
But This only works if my files are in this path: src/main/groovy or src/test/groovy
Now lets say my files are in src/core or not even in the src folder itself.
How can I tell CodeNarc the new paths.

According to the CodeNarc Plugin docs, you have to give it particular SourceSets.

According to the SourceSets docs, you could create a custom SourceSet with something like this:


...

sourceSets {
    narcOn {
        groovy {
            srcDir file('foo/src/bar/groovy') /*...If you want to add just one custom one...*/
        }
    }
}
...
codenarc{ 

    ...
    sourceSets narcOn /* not strictly neccessary; codenarc should automatically detect it without this*/
    ...

}

OR

...

sourceSets {
    narcOn {
         groovy {
            srcDirs = ['foo', 'fizz', 'buzz'] /*...If you want more than one custom ones...*/
         }
    }
    ...
}
...

OR

...

sourceSets {
    main {
        groovy {
            srcDir file('src/core') /*...If you want to add a custom one to the conventional 'main' one...*/
        }
    }
}
...

That’ll get you codenarc tasks you could call with either gradle codenarcNarcOn... or gradle codenarcMain....

If that doesn’t work for you, please get back to share whichever solution you do finally end up doing? Thanks.

1 Like

Hi,
Seems like all your examples are working. I ended up using the following:

sourceSets {
    main {
        groovy {
	    srcDirs = ['src/main', 'vars']
        }
    }   
}

Thanks

1 Like

You’re welcome :slight_smile: Thanks for letting us know it works :+1: