Is there a way to add jars to the classpath for the Gradle CodeNarc plugin?

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. Chris

Is there an answer/resolution to this question?

You’d have to add the required dependencies to the ‘codenarc’ configuration, in addition to CodeNarc itself.

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.

dependencies {
    codenarc ...

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.

Why codenarc plugin can’t just inherit project’s dependencies?

This could potentially destabilise codenarc as your applications dependencies may interfere with codenarc’s.

To do this properly we need to set this up as a different classpath and give that to codenarc which it can isolate. Does codenarc support this?

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:

dependencies {
 codenarc 'somegroup:artifact:123'

with no success. There is an error:

$ gradle clean check
: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:

@java -classpath %GROOVY_JAR%;lib/CodeNarc-0.5.jar;lib/log4j-1.2.14.jar;lib org.codenarc.CodeNarc %*

I don’t how it relates to gradle/maven plugins though.

If you add a dependency to the codenarc configuration you also need to add the actual codenarc dependency.

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.