IntellijIDEA not recognizing classes specified in

2019-01-31 11:12发布

问题:

I have a project in IntellijIDEA which was created with Maven. I then specified a set of dependencies and external repositories in the Pom.xml file.

The project builds fine on command line if I do "mvn install". When I open any of the code files in the IDE though it says all the classes handled by Maven dependencies aren't recognized - as it would for a normal project if I never added the required JARs to the build path.

I know in my Eclipse Maven projects (rather than IntelliJ) it usually shows an extra directory on the left which says "Maven Dependencies" and lists the JARs pulled in via maven. I don't see that here. What am I doing wrong?


Here's what my screen looks like:

回答1:

For some reason re-import did not do the trick. After looking at this:

http://www.jetbrains.com/idea/webhelp/maven-importing.html

I set auto-import and then the problem went away though; hopefully it helps someone else. Thanks for the ideas everyone :).



回答2:

After installing IntelliJ IDEA on a new computer I found myself with the same issue.

I had to update the remote maven repository. (Settings > Maven > Repositories)

Both local and remote repos needed to be updated. The remote one wasn't updated ever before this. After a restart everything worked fine. You might have to reimport your project.



回答3:

I was running into similar issues. For me it ended up being that I was importing the project incorrectly. I had been doing

import project
    <navigate to existing project and click top level directory>
    follow the wizard

What solved my maven problems was to do

import project
    <navigate to existing project and click the pom.xml
    follow the wizard


回答4:

You could go to:

File > Settings > Build, Execution, Deployment > Build Tools > Maven

and check if your pom.xml is not in the "Ignored Files" list.



回答5:

A simple reimport and/or update of the repositories via Intellij did not do the trick for me.

Instead I had to delete the complete ~/.m2/repository directory and let maven sort everything out by itself. Afterwards Maven -> Reimport finished it off.



回答6:

For me File>>Invalidate Cache/Restart>>Invalidate and Restart worked in IntelliJ



回答7:

In my case the problem was that the project was in maven2 while intellj was configured for maven3. Switching to maven2 in settings solved the problem



回答8:

Go to File > Settings > Build, Execution, Deployment > Build Tools > Maven and check the Maven home directory. This should be the same maven installation used for command line



回答9:

Might be useful to others that were still stuck like me. None of the suggested fix worked. Actually, not before I fixed my main problem which was the installation location of maven.

In my case, I did not use the standard location. Changing that location in the maven settings (Settings/Maven/Maven home repository) did the trick.

My 2 cents.



回答10:

This also happened to me after upgrading Intellij to 1.4.15. I tried to re-import the whole project with same result, but enabling Maven Auto Import did the tricks.



回答11:

Looks like there are several, valid reasons why intelliJ would ignore a pom file. None of the previous answers worked in my case, so here's what did work, in case someone else runs into this issue:

In this example, module3 was being completely ignored by IntelliJ. The pom.xml in that directory wasn't even being treated as a maven pom.

My project structure is like this:

myProject
    module1
    module2
    module3

BUT, my (simplified) pom structure is like this:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>devs</groupId>
    <artifactId>myProject</artifactId>
    <version>0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>myProject</name>

    <modules>
        <module>module1</module>
        <module>module2</module>
    <modules>

    <profiles>
        <profile>
            <id>CompleteBuildProfile</id>
            <modules>
                <module>module3</module>
            </modules>
        </profile>
    </profiles>
</project>

To fix this, I modified the root <modules> element to add in module3 temporarily.

    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    <modules>

Next re-import the root pom, and IntelliJ will add the module. When that's done, revert the pom. IntelliJ will ask if you also want to remove module3 from the project structure. Click No.

Bam! Done. Module3 works and I can run my Integration tests from IntelliJ again :D



回答12:

The problem was caused for me by selecting the project directory to be Imported when first starting IntelliJ rather than the pom.xml file for the project.

Closing the problem project and then following the Import process again but choosing the pom.xml resulted in a fully working project in the IDE.



回答13:

For me the problem seems to be a conflict with the maven helper plugin (https://plugins.jetbrains.com/plugin/7179?pr=idea).

I disable it, and it works again :)



回答14:

For me, what did the trick was to add the dependencies in File > Project Settings > Modules > Dependencies.