IntelliJ IDEA 和 Gradle-不能应用于’(groovy.lang. Closure)’

我有一个 Gradle 文件,每当我在 IntelliJ IDEA 14.1.5中打开它时,它都会显示整个文件的 IDE 错误。也就是说,所有的错误似乎都是:

Lang. 字符串错误

java.lang.String errors

或者

Groovy.lang 关闭错误

groovy.lang.Closure errors

我试过清除文件的内容,只写了最上面一行:

group 'com.me.blah'

但即使这样也会导致错误。

出于上下文的考虑——这是一个较大的 SpringBoot 项目中的单个模块。这个模块是一组简单的静态文件(包括用于 CSS 编译、静态分析等的 Gradle) ,而其余的都是 Java 模块,没有有 Gradle 问题。

有人能想到为什么 IntelliJ 会挣扎着去理解这个 Gradle 文件吗?

77353 次浏览

For anyone looking for a similar fix, this boiled down to the type of the module. My module was defined in my .iml file as

type="WEB_MODULE"

I created a new Gradle module and pasted in the same contents, deleted the original, renamed the new module to have the same name as the old one, and everything worked fine. When I diffed the results the only change was that the .iml file now said:

type="JAVA_MODULE"

So there's the answer, seemingly. Change your module from "web" to "java".

I've noticed this often. When creating a new project and using the 'Gradle' template, IntelliJ seems to get it all wrong. So what I've resorted to doing is after creating a new project, delete the .idea and .iml files and re-open the project directly from the Gradle file. IntelliJ seems to better understand what is going on in this case.

I found this suggestion in the IDEA bug tracker(IDEA-142683):

Workaround:
One can add a comment such as
//noinspection GroovyAssignabilityCheck
to suppress the warning, but this should not be necessary as these are standard every day Gradle usages.

This issue was recently updated and is marked fixed, ready for release with version 2016.2 release.

Happened to me recently on windows. I tried all of the above but it didn't work.

What i did:

1 - I had JAVA_HOME environment variable already set. So IDEA picked that as project SDK and these warnings showed up.

2 - So, i removed the default JAVA_HOME from project SDK and manually added jdk path. Restarted and all warnings were gone.

Maybe it will help someone. Thanks.

For the first part of the question: group is a property, so there must be an assignment, not a function call. The right code is:

group = 'com.me.blah'

What I found is that this warning is from Groovy inspection of the Intelliji. So if your project is not pure groovy, you can disable this inspection by going to File -> settings -> Editor -> Inspections -> disable Groovy. Again, just a workaround.

Try File | Invalidate Caches and restart. It worked for me after i mess the syntaxe somewhere else in the file.

For me, after trying out all this answers without result, changing the Java SDK of the project did the trick, I was on 1.8 and changed it to the newest one, but still a project level language of 8.

Hope this helps!