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

Answered

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.

13 comments
Comment actions Permalink

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

0
Comment actions Permalink

Yes, it is!

0
Comment actions Permalink

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

0
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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

1
Comment actions Permalink

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
Comment actions Permalink

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

0
Comment actions Permalink

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

0
Comment actions Permalink

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
Comment actions Permalink

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

Please sign in to leave a comment.