Is there a way to add jars to the classpath for the Gradle CodeNarc plugin?
Beginning with version 0.19 of CodeNarc, it includes some rules that use a later compilation phase to produce a richer AST, but that requires the application jars and dependencies to be on the CodeNarc classpath. So, these rules seem to work ok (so far) for running CodeNarc against the production code (codenarcMain), but fails when running against the test code (codenarcTest) because the JUnit jar is not on the classpath,
Thanks for the reply, Peter. What is the correct way to “add the required dependencies to the codenarc configuration”? I tried a couple things with no effect, but that is probably just my lack of experience.
I was about to post the exact same question. This issue is kind a pain. BTW. the same happens with codenarc maven plugin.
Of course I’ll try Peter’s suggestion in a moment but I have some doubts about such approach. Why codenarc plugin can’t just inherit project’s dependencies? As those mentioned rules going to fail on compile with any class referring to external class, all compile-scoped dependencies have to be duplicated within codenarc scope inside the build.gradle file. That’s doesn’t seem ‘nice’, does it?
There is a way to overcome this. Run codenarc as unit test without the plugin at all (no matter which build tool). Then it gets all necessary jars on the classpath, but it’s kind of hackery. I still prefer to use plugin, though I had to disable the mentioned rules.
I understand the risk of mixed classpaths, but at least there could be a kind of flag “inherit project’s dependencies” true/false for the plugin settings. I guess there will be no problem with inheriting dependencies by the plugin in the most cases.
Speaking about the codenarc I’m not sure. The plugin itself doesn’t seem to have any option of passing classpath settings, unless there is general way for gradle plugins. I tried Peter’s suggestion as follows:
$ gradle clean check
:clean
:codenarcMain FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':codenarcMain'.
> java.lang.ClassNotFoundException: org.codenarc.ant.CodeNarcTask
When static analysis with codenarc is run as unit test then the same classpath is available for it as for the unit test itself. When run from command line, just regular classpath switch is passed:
Thanks, that worked in scope of the last error I wrote about. However it seems that codenarc plugin doesn’t support such approach.
I even debug the codenarc code to see what’s the compilation error. As I thought the problem is with import statement for external class. However when I printed dependencies for codenarc configuration it has the required jar there.