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.
Please sign in to leave a comment.
Is project/src/test/java configured as test source root?
Yes, it is!
Mike, could you please create a sample project that shows the problem?
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.
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.
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.
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/.
I did File -> Invalidate Caches/Restart and it solved the problem
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?
Please file a bug with the sample project and the steps to reproduce.
I was also facing the same problem, For me it solved when i performed Invalidate Caches/Restart.
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?
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.
Still facing the same issue
@... 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 ...)
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.
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.
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.
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?
Serge Baranov I don't have a link to a sample of Jhipster Maven. My log shows the following errors.
My life with Intellij right now resumes to this:
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.
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
@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.
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
@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.
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
@Alvarohispania your solution helped me also, I have also a spring boot and gradle project.
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
>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?