简体   繁体   English

Codenarc 无法解析 groovy 类

[英]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.我正在使用带有 gradle 的 codenarc 1.4 来测试 Jenkins 共享库中的 groovy 代码,但是在运行它时输出错误,说它无法解析groovy.lang.Closure虽然这似乎并没有阻止运行检查。

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:当 codenarc 运行时,会产生以下错误:

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:我的 gradle 配置的 codenarc 部分如下:

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.我可以通过添加import groovy.lang.Closure来停止错误消息,但这会导致UnnecessaryGroovyImport规则出错。 Is there a way to prevent these errors being reported without removing the UnnecessaryGroovyImport rule?有没有办法在不删除UnnecessaryGroovyImport规则的情况下防止报告这些错误?

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:我还使用 CodeNarc 和 Gradle 进行 Jenkins 共享库测试,发现将以下内容添加到我的 Gradle 配置中修复了类似的问题:

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.我在 GitHub 上对 CodeNarc 问题的评论中发现了这一点。

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.我认为问题与每个文件都被单独编译以进行静态分析有关,这与 Gradle 中的正常编译步骤不同。 I'm very new to this though so maybe misunderstanding it.我对此很陌生,所以可能会误解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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