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.

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

1
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

0

Please sign in to leave a comment.