简体   繁体   中英

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

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 :

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. But in IntelliJ, I have added JUnit to the classpath and the editor is certainly able to autocomplete aspects of JUnit. Further, in External Libraries tree under the Project Explorer, I see that JUnit4 is listed as a library.

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). Further, I'm using subprojects in the main build.gradle file.

I'm not sure what else to check at this point. Nothing in the IDE indicates errors. Nothing in my Gradle build files seem to be generating errors. 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. I then find that my External Libraries no longer show JUnit4. Then the LucidRunner.java file all the sudden springs up with a series of errors about JUnit.

So I Option+Enter and select "Add JUnit4 to Classpath".

That adds JUnit4 back to External Libraries and my errors disappear.

But the gradle build command still shows the same error. 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.

Well, I seem to have answered my own question.

In the build.gradle file that is in the lucid-junit project, I had this:

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".

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.

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