简体   繁体   English

Gradle项目中无法识别类路径

[英]Classpath Not Recognized in Gradle Project

I have a project branch here that shows my issue: 我在这里有一个项目分支来显示我的问题:

https://github.com/testerstories/lucid-core/tree/feature/build-full-archive https://github.com/testerstories/lucid-core/tree/feature/build-full-archive

Specifically, if I run gradle build , then when the :lucid-junit:compileJava task runs, I get a series of errors like this on my only class ( LucidRunner ) in src/main/java/com/lucidtester/junit/runners : 具体来说,如果我运行gradle build ,那么当:lucid-junit:compileJava任务运行时,我在src/main/java/com/lucidtester/junit/runners唯一类( LucidRunner )上会收到一系列类似的错误:

package org.junit.runner.notification does not exist
import org.junit.runner.notification.RunNotifier;

package org.junit.runners does not exist
import org.junit.runners.BlockJUnit4ClassRunner;
                    ^
package org.junit.runners.model does not exist
import org.junit.runners.model.FrameworkMethod;
                          ^
package org.junit.runners.model does not exist
import org.junit.runners.model.InitializationError;

That would seem to imply that JUnit is not being recognized. 这似乎意味着未识别JUnit。 But in IntelliJ, I have added JUnit to the classpath and the editor is certainly able to autocomplete aspects of JUnit. 但是在IntelliJ中,我已经将JUnit添加到类路径中,并且编辑器当然能够自动完成JUnit的各个方面。 Further, in External Libraries tree under the Project Explorer, I see that JUnit4 is listed as a library. 此外,在Project Explorer下的External Libraries树中,我看到JUnit4被列为一个库。

The same thing happens if I restrict the build to the particular module, by running this command: 如果通过运行以下命令将构建限制为特定模块,则会发生相同的情况:

gradle :lucid-junit:build

At this point, I have absolutely no idea what the problem is. 在这一点上,我完全不知道问题是什么。 I only have a minimal class and my build file (in the module) is putting in the correct dependency (for junit). 我只有一个最小的类,并且我的构建文件(在模块中)放入了正确的依赖项(对于junit)。 Further, I'm using subprojects in the main build.gradle file. 此外,我在build.gradle主文件中使用了subprojects

I'm not sure what else to check at this point. 我不确定目前还需要检查什么。 Nothing in the IDE indicates errors. IDE中没有任何内容表示错误。 Nothing in my Gradle build files seem to be generating errors. 我的Gradle构建文件中似乎没有任何东西会产生错误。 But it seems like something about how the build lifecycle is executing is not recognizing my classpath. 但是似乎有关构建生命周期如何执行的某些事情无法识别我的类路径。

UPDATE 更新

Found something interesting. 发现了一些有趣的东西。 If I change my build file at all, IntelliJ refreshes everything. 如果我完全更改了构建文件,则IntelliJ会刷新所有内容。 I then find that my External Libraries no longer show JUnit4. 然后,我发现我的外部库不再显示JUnit4。 Then the LucidRunner.java file all the sudden springs up with a series of errors about JUnit. 然后,LucidRunner.java文件突然冒出了一系列有关JUnit的错误。

So I Option+Enter and select "Add JUnit4 to Classpath". 因此,我按Option + Enter并选择“将JUnit4添加到类路径”。

That adds JUnit4 back to External Libraries and my errors disappear. 这将JUnit4添加回外部库,我的错误消失了。

But the gradle build command still shows the same error. 但是gradle build命令仍然显示相同的错误。 If I change the build file again, same situation. 如果我再次更改构建文件,则情况相同。

I have tried what seems to be the "reboot" answer for IntelliJ: invalidate caches and restart, but that has no effect. 我已经尝试了IntelliJ的“重新启动”答案:使缓存无效并重新启动,但这没有任何效果。

Well, I seem to have answered my own question. 好吧,我似乎已经回答了我自己的问题。

In the build.gradle file that is in the lucid-junit project, I had this: 在lucid-junit项目的build.gradle文件中,我有以下内容:

dependencies {
  compile "junit:junit:${junit_version}"
}

What I had to do was add a blank line at the top of the file. 我要做的是在文件顶部添加一个空白行。

I have no idea why that matters but when I do that, all the sudden everything works. 我不知道为什么这很重要,但是当我这样做时,所有突然的事情都会起作用。 Further, with that blank line in place, in my External Library I now don't see just "JUnit4" but rather "Gradle: junit:junit:4.12". 此外,有了该空白行,在我的外部库中,我现在不仅看到“ JUnit4”,还看到了“ Gradle:junit:junit:4.12”。

If I remove that initial blank line, the problem reappears. 如果我删除了最初的空白行,问题再次出现。

Again, I have no idea why this works but there is absolutely no doubt that it does. 再说一次,我不知道为什么会这样,但是绝对没有疑问。

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

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