Why do I constantly have to add a library to the classpath?

已回答

I've been working on a Maven project for a while now.

In my parent POM, I have the following:

            <dependency>
                <groupId>net.project.class</groupId>
                <artifactId>java-test-utils</artifactId>
                <version>1.0.8-SNAPSHOT</version>
                <type>bundle</type>
            </dependency>

In my POM, I have the following:

        <dependency>
            <groupId>net.project.class</groupId>
            <artifactId>java-test-utils</artifactId>
            <type>bundle</type>
            <scope>test</scope>
        </dependency>

Maven resolves this just fine, but every so often (seems like when IDEA is "resolving dependencies"), I'll start getting errors in my Java classes. In this particular case, it's under project/src/test/java/...

The only way to fix the issue in IDEA is to do Alt+Enter (I'm on Mac), and choose "Add library 'Maven: net.project.class:java-test-utils:bundle:1.0.8-SNAPSHOT' to classpath"

And then when I'm resolving dependencies again, I have to add it to the classpath again! Is there a solution to this? Is this a bug?

There are other times where instead of adding something to the classpath, I have to "Add dependency on module 'other-module'".

This is occurring since IDEA X and is present in 10.0.1, 10.0.2, 10.0.3, and 10.5.

评论操作 固定链接

Is project/src/test/java configured as test source root?

0
评论操作 固定链接

Mike, could you please create a sample project that shows the problem?

0
评论操作 固定链接

I'll need some time to create one (if I can).

I believe that the issue is that maven recognizes the maven-bundle-plugin that generates OSGi bundles. IDEA does not, so it cannot import a maven module of <packaging>bundle</packaging>. That means that it needs to add the library because it can't get it from the POM file.

-1
评论操作 固定链接

I'm getting this exact same thing....it's been 7 years and this is still an issue? I'm not using OSGi. I'm simply using some Spring/Boot annotations. IntelliJ loses it's mind at random (seemingly) and I have to re-add the maven library that I already added. There's got to be a solution for this. I can't imagine this problem escaping such a large community for so long.

2
评论操作 固定链接

Please report at https://youtrack.jetbrains.com/issues/IDEA with the sample project and the steps to reproduce so that we can fix the issue.

0
评论操作 固定链接

I started working with Maven last week and also had problems with dependencies not being added to the classpath. Eventually I went back to scratch and set up my project again with different settings (make sure to select "Import project from external model -> Maven") and everything has been working seamlessly since then. I wrote a blog post about it at https://katebutler.nz/2018/10/29/why-intellij-wouldnt-import-my-maven-dependencies/.

0
评论操作 固定链接

I did File -> Invalidate Caches/Restart and it solved the problem

1
评论操作 固定链接

I'm facing the same problem with both Maven and Gradle application. I tried Invalidate Caches/Restart, clearing the .m2 repository and reimporting the project but none of them works consistently. Sometimes they works and sometimes not. Please let me know if there is any permanent solution available. Also what causes this problem?

0
评论操作 固定链接

Please file a bug with the sample project and the steps to reproduce.

0
评论操作 固定链接

I was also facing the same problem, For me it solved when i performed Invalidate Caches/Restart.

0
评论操作 固定链接

I also face this issue many times. I hate IntelliJ for this. I recently moved to Intellij from STS because we only had a license for IntelliJ and I am already hating it.

Why can't IntelliJ see what has been added and resolved by maven. Why I am required to add all the libraries one at a time?  

0
评论操作 固定链接

Gagan Suri all the libraries are imported from Maven automatically. If it doesn't happen, it's a bug and you should provide the logs so that we can fix it: https://intellij-support.jetbrains.com/hc/articles/207241085. Or there is some misconfiguration that prevents Maven from downloading the dependencies. You should never add them manually in the IDE one by one as all the changes will be discarded on the next reimport of the Maven or Gradle project. There is a warning about that shown in the module dependencies dialog.

0
评论操作 固定链接

Still facing the same issue 

0
评论操作 固定链接

Hitesh7garg Can you please file a bug at https://youtrack.jetbrains.com/issues/IDEA, and provide a small project sample along with the idea.log file? (Help | Show Log in ...)

0
评论操作 固定链接

10 years after this thread was created, and this problem persists. I just imported a maven project, and I'm having to add all libraries to classpath manually. I recently upgraded my IntelliJ from version 2019 which worked perfectly to version 2021, and IntelliJ just don't import my dependencies. I have been trying and re-trying to import the project for 2 hours, waiting for the miracle of the import, but it just doesn't work. In the previous version of Intellij I used I never had this problem. Can you guys fix this issue, please? I'm using IntelliJ IDEA 2021.1.3 (Ultimate Edition) Build #IU-211.7628.21, built on June 30, 2021.

0
评论操作 固定链接

Tharso Ferreira

See the comment above: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206927425/comments/360001615959

If a dependency was not added automatically, it is a bug or misconfiguration. Please share a sample project and logs. 

0
评论操作 固定链接

Konstantin Annikov

since 2015 I use Intellij at work and for my personal projects, and I never had to do anything special for Intellij to load my maven projects correctly, that's one of the reasons I'm with you guys, but After almost 1 year without updating, to avoid this kind of problem, I finally decided to update, and I wasn't surprised that something wasn't working. I don't believe it's a misconfiguration, I believe somewhere is wrong. 

You can create a Java(Maven) project using Jhipster and try to import, it just doesn't work. I pay an IDEA because it makes me more productive, but what's happening here is just the opposite, hours wasted with the same problem. This is ridiculous. 

You guys should be able to find this error without us having to provide anything.

1
评论操作 固定链接

Tharso Ferreira, do you have any link to a sample JHipster Maven project that doesn't work for you? Are there any errors in the logs after you import the project? See https://intellij-support.jetbrains.com/hc/en-us/articles/207241085. What specific errors do you get with the project?

0
评论操作 固定链接

Serge Baranov I don't have a link to a sample of Jhipster Maven. My log shows the following errors.

 

2021-08-15 21:37:28,901 [  10619]  ERROR - Container.ComponentManagerImpl - workspace option is deprecated (implementationClass=org.google.code.translate.TranslateConfiguration) 
2021-08-15 21:37:28,909 [ 10627] ERROR - Container.ComponentManagerImpl - IntelliJ IDEA 2021.2 Build #IU-212.4746.92
2021-08-15 21:37:28,914 [ 10632] ERROR - Container.ComponentManagerImpl - JDK: 11.0.11; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-08-15 21:37:28,914 [ 10632] ERROR - Container.ComponentManagerImpl - OS: Mac OS X
2021-08-15 21:37:28,914 [ 10632] ERROR - Container.ComponentManagerImpl - Last Action:
2021-08-15 21:37:50,187 [ 31905] ERROR - Container.ComponentManagerImpl - workspace option is deprecated (implementationClass=org.google.code.translate.TranslateConfiguration)
2021-08-15 21:37:50,190 [ 31908] ERROR - Container.ComponentManagerImpl - IntelliJ IDEA 2021.2 Build #IU-212.4746.92
2021-08-15 21:37:50,190 [ 31908] ERROR - Container.ComponentManagerImpl - JDK: 11.0.11; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-08-15 21:37:50,191 [ 31909] ERROR - Container.ComponentManagerImpl - OS: Mac OS X
2021-08-15 21:37:50,191 [ 31909] ERROR - Container.ComponentManagerImpl - Last Action:
2021-08-15 21:38:56,120 [ 97838] ERROR - jediterm.terminal.TerminalMode - Mode EightBitInput is not implemented, setting to true
2021-08-15 21:38:56,121 [ 97839] ERROR - jediterm.terminal.TerminalMode - Mode EightBitInput is not implemented, setting to true
2021-08-15 21:38:56,121 [ 97839] ERROR - jediterm.terminal.TerminalMode - Mode EightBitInput is not implemented, setting to true
2021-08-15 21:48:17,754 [ 659472] ERROR - Container.ComponentManagerImpl - workspace option is deprecated (implementationClass=org.google.code.translate.TranslateConfiguration)
2021-08-15 21:56:21,499 [1143217] ERROR - aemon.impl.PassExecutorService - IntelliJ IDEA 2021.2 Build #IU-212.4746.92
2021-08-15 21:56:21,499 [1143217] ERROR - aemon.impl.PassExecutorService - JDK: 11.0.11; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-08-15 21:56:21,499 [1143217] ERROR - aemon.impl.PassExecutorService - OS: Mac OS X
2021-08-25 23:05:56,946 [869318664] ERROR - leEditor.impl.EditorsSplitters - Assertion failed java.lang.Throwable: Assertion failed
2021-08-25 23:44:00,123 [871601841] ERROR - leEditor.impl.EditorsSplitters - IntelliJ IDEA 2021.2 Build #IU-212.4746.92
2021-08-25 23:44:00,129 [871601847] ERROR - leEditor.impl.EditorsSplitters - JDK: 11.0.11; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-08-25 23:44:00,129 [871601847] ERROR - leEditor.impl.EditorsSplitters - OS: Mac OS X
2021-08-25 23:44:00,137 [871601855] ERROR - leEditor.impl.EditorsSplitters - Last Action: ViewNavigationBar
2021-08-25 23:53:36,684 [872178402] ERROR - til.concurrency.QueueProcessor - IntelliJ IDEA 2021.2 Build #IU-212.4746.92
2021-08-25 23:53:36,684 [872178402] ERROR - til.concurrency.QueueProcessor - JDK: 11.0.11; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-08-25 23:53:36,684 [872178402] ERROR - til.concurrency.QueueProcessor - OS: Mac OS X
2021-08-25 23:53:36,687 [872178405] ERROR - til.concurrency.QueueProcessor - Last Action: CommentByBlockComment
2021-08-25 23:53:36,687 [872178405] ERROR - til.concurrency.QueueProcessor - Current Command: Comment with Block Comment
2021-08-25 23:54:25,330 [872227048] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 10 times
2021-08-25 23:55:10,364 [872272082] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 20 times
2021-08-25 23:56:00,412 [872322130] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 30 times
2021-08-25 23:56:45,465 [872367183] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 40 times
2021-08-25 23:57:30,494 [872412212] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 50 times
2021-08-25 23:58:15,528 [872457246] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 60 times
2021-08-25 23:59:05,558 [872507276] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 70 times
2021-08-25 23:59:55,602 [872557320] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 80 times
2021-08-26 00:00:40,663 [872602381] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 90 times
2021-08-26 00:01:25,718 [872647436] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 100 times
2021-08-26 00:02:10,778 [872692496] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 110 times
2021-08-26 00:03:05,879 [872747597] ERROR - llij.ide.plugins.PluginManager - Exception 'java.lang.AssertionError' was reported 120 times
0
评论操作 固定链接

My life with Intellij right now resumes to this: 

0
评论操作 固定链接

We'll need the complete logs and a sample project to investigate it further, feel free to file a bug at https://youtrack.jetbrains.com/newIssue?project=IDEA.

0
评论操作 固定链接

Not sure if this will be helpful to anyone, but since i faced this problem Today and spent quite a bit of time trying to solve it, i thought that maybe someone would be in a similar situation and could benefit from how i solved it. Basically i went to open module settings for the project and in my case, besides the top m odule with its name, there were another two modules one for main and one for test (its a spring boot project i imported with the option of external model maven), i removed main and test from the list and instantly all the different classes that were highlighted in red were automatically recognized. Seems like those 2 additional "modules" were confusing intellij. Hope it helps

7
评论操作 固定链接

@Alvarohispania main and tests modules are the standard gradle source sets. IDE expectedly creates for them modules, see https://stackoverflow.com/a/69287021/2000323 It is not recommended to modify project structure manually in a Maven/Gradle based projects. IDE will re-create the structure on the next Maven/Gradle project reload.

0
评论操作 固定链接

Andrey Dernov i think there might be some inconsistency on how maven projects are imported. Another project with almost identical structure when imported it only had as module the top one, not the main or test ones. That other project didn't have any issue. I have also talked to other coworker and he doesnt have and has never had the main and test modules showing up in the modules section of Module Settings fo the same projects. I only had them showing up in one but it was what was causing the classes not being loaded

1
评论操作 固定链接

@Alvarohispania I though the project you have is a Gradle-based. If the project is a Maven-based IDE indeed does not create additional main and test modules and it may be some issue. If you encounter the problem again, it would be very nice if you could describe how we could reproduce, a sample project and IDE logs (can be attached privately in a YouTrack issue). Thank you.

0
评论操作 固定链接

hello if anyone have this problem , i found a solution  basically if u have test and main package in gradle.build  u need to 

have implementation for the test and for the main example 

dependencies {
testImplementation 'io.cucumber:cucumber-java:7.0.0'
implementation 'io.cucumber:cucumber-java:7.0.0'

}

this fixed my problem

0
评论操作 固定链接

@Alvarohispania your solution helped me also, I have also a spring boot and gradle project.

0
评论操作 固定链接

Hi guys,

I'm resolving the exact same issue. 

This is my context: I generated a fresh project from IntelliJ, I developed during hours and when I added a new dependency in pom.xml, suddently all of my Spring annotation became red. 

What I discovered >> File > Project Settings > dependencies. I took a look at my dependencies. In my case the Scope of my dependencies is Test. 

Serge Baranov, Andrey Dernov, Konstantin Annikov This may help you about the root cause.

kr

0
评论操作 固定链接

>I took a look at my dependencies. In my case the Scope of my dependencies is Test. 

Thanks for the feedback. Could you clarify, what are those dependencies exactly - do they have Maven prefix? What is their corresponding declaration in pom.xml file?

Also, what dependency have you added? 

0

请先登录再写评论。