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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32905270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:00:20  来源:igfitidea点击:

IntelliJ IDEA and Gradle - Cannot be applied to '(groovy.lang.Closure)'

javaintellij-ideagroovygradlebuild.gradle

提问by Seb Charrot

I have a Gradle file which, whenever I load open it in IntelliJ IDEA 14.1.5, shows IDE errors for the entire file. Namely all the errors seem to be either:

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

java.lang.String errors

java.lang.String 错误

java.lang.String errors

java.lang.String 错误

or

或者

groovy.lang.Closure errors

groovy.lang.Closure 错误

groovy.lang.Closure errors

groovy.lang.Closure 错误

I've tried clearing the file's contents and only writing the top line:

我试过清除文件的内容,只写第一行:

group 'com.me.blah'

but even that results in an error.

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

For context's sake - this is an individual module in a larger SpringBoot project. This module is a set of simple static files (with Gradle for CSS compilation, static analysis, etc), while the rest are Java modules, and are nothaving Gradle issues.

就上下文而言 - 这是更大的 SpringBoot 项目中的一个单独模块。这个模块是一组简单的静态文件(带有用于 CSS 编译、静态分析等的 Gradle),而其余的都是 Java 模块,并且没有Gradle 问题。

Can anyone think why IntelliJ would be struggling to understand this Gradle file?

谁能想到为什么 IntelliJ 会努力理解这个 Gradle 文件?

采纳答案by Seb Charrot

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

对于任何寻求类似修复的人来说,这归结为模块的类型。我的模块在我的 .iml 文件中定义为

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:

我创建了一个新的 Gradle 模块并粘贴了相同的内容,删除了原始模块,将新模块重命名为与旧模块同名,一切正常。当我比较结果时,唯一的变化是 .iml 文件现在说:

type="JAVA_MODULE"

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

答案似乎是这样的。将您的模块从“web”更改为“java”。

回答by Jon Nichols

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.

我经常注意到这一点。在创建新项目并使用“Gradle”模板时,IntelliJ 似乎完全搞错了。所以我采取的做法是在创建一个新项目后,删除 .idea 和 .iml 文件,然后直接从 Gradle 文件重新打开项目。IntelliJ 似乎更好地理解在这种情况下发生了什么。

回答by FGreg

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

我在 IDEA 错误跟踪器 ( 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.

解决方法:
可以添加注释
//noinspection GroovyAssignabilityCheck
以抑制警告,但这应该不是必需的,因为这些是 Gradle 每天的标准用法。

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

此问题最近已更新,并标记为已修复,准备与 2016.2 版一起发布。

回答by Rajkishan Swami

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

最近在windows上发生在我身上。我尝试了上述所有方法,但没有奏效。

What i did:

我做了什么:

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

1 - 我已经设置了 JAVA_HOME 环境变量。所以 IDEA 选择了它作为项目 SDK 并且出现了这些警告。

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

2 - 所以,我从项目 SDK 中删除了默认的 JAVA_HOME 并手动添加了 jdk 路径。重新启动,所有警告都消失了。

Maybe it will help someone. Thanks.

也许它会帮助某人。谢谢。

回答by Beholder

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

对于问题的第一部分:group是一个属性,所以必须有一个赋值,而不是一个函数调用。正确的代码是:

group = 'com.me.blah'

回答by Neal Sha

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.

我发现这个警告来自于对 Intelliji 的 Groovy 检查。因此,如果您的项目不是纯 groovy,您可以通过转到文件 -> 设置 -> 编辑器 -> 检查 -> 禁用 Groovy 来禁用此检查。同样,这只是一种解决方法。

回答by Bancarel Valentin

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

尝试文件 | 使缓存无效并重新启动。在我弄乱文件中其他地方的语法之后,它对我有用。

回答by Ariel Mirra

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.

对我来说,在尝试了所有这些答案而没有结果后,更改项目的 Java SDK 成功了,我在 1.8 上并将其更改为最新版本,但仍然是 8 的项目级语言。

Hope this helps!

希望这可以帮助!