简体   繁体   中英

Codenarc unable to resolve groovy classes

I am using codenarc 1.4 with gradle to test groovy code in a Jenkins shared library, but when running it outputs errors saying it was unable to resolve groovy.lang.Closure though this doesn't seem to prevent the checks being run.

An example of the code that's hitting the problem is

interface IStepExecutor {
    int sh(String command)
    void dir(String path, Closure commands)
    ...

when codenarc is run the following error is produced:

file:/.../IStepExecutor.groovy: 8: unable to resolve class Closure 
 @ line 8, column 27.
       void dir(String path, Closure commands)
                             ^

The codenarc parts of my gradle config is as follows:

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.5.7'
    testCompile "org.spockframework:spock-core:1.3-groovy-2.5"
}
codenarc {
    toolVersion = "1.4"
}
codenarcMain {
    configFile = file("config/codenarc/CodeNarcMain.groovy")
    source = 'src'
    compilationClasspath += files('src/')
}
codenarcTest {
    configFile = file("config/codenarc/CodeNarcMain.groovy")
    source = 'src'
    compilationClasspath += files('src/')
}

I can stop the error messages by adding an import groovy.lang.Closure but that causes the UnnecessaryGroovyImport rule to error. Is there a way to prevent these errors being reported without removing the UnnecessaryGroovyImport rule?

I'm also using CodeNarc with Gradle for Jenkins shared library tests and found adding the following to my Gradle configuration fixed a similar issue:

codenarcMain {
    compilationClasspath = sourceSets.main.compileClasspath + sourceSets.main.output
}

codenarcTest {
    compilationClasspath = codenarcMain.compilationClasspath + sourceSets.test.compileClasspath + sourceSets.test.output
}

I found this in a comment on a CodeNarc issue on GitHub.

I think the problem is something to do with each file being compiled in isolation for static analysis which is different from the normal compilation steps in Gradle. I'm very new to this though so maybe misunderstanding it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM