Hello, everyone, I’m the little black man downstairs ~

When we run Maven projects using IDEA locally, we sometimes have the bad luck of encountering some Maven dependencies that cannot be found and imported properly. This leads to a bunch of eye-catching red errors when IDEA builds the project.

That recently small black brother new pull a project source code, run the project locally, and encounter this situation. According to the previous experience, tamper with, found no way to solve. Then studied one afternoon, finally solve this problem 🤔.

Just for fun, at first I thought there was something wrong with this project, but I found that his computer could run the project normally.

In today’s article, I will summarize the reason why IDEA failed to find related dependencies when running maven projects and how to solve the problem.

Network problem, cannot download Maven dependency

Maven’s default central repository address is search.maven.org/#browse, so this is…

And our domestic access to external network address, network speed is not only slow, network quality is poor. As a result, Maven relies on slow downloads and sometimes fails.

Maven dependencies failed to download and IDEA could not find the dependencies.

It is easy to solve this problem by setting the mirrors address.

${user.home}/.m2/settings. XML. Then find the mirrors node and add the following configuration below:

<mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
</mirrors>
Copy the code

The lastUpdated file caused a dependency import failure

When you update the image address, it is possible to run into maven dependencies that cannot be imported, so congratulations on the second problem.

When maven relies on downloads, if the download fails due to a poor network, maven will generate files locally with the suffix lastUpdated.

The normal Maven dependencies are as follows

The files with lastUpdated as the suffix have the following functions:

Maven dependencies will not be downloaded again as long as the lastUpdated file exists, so simply delete the lastUpdated file and refresh it.

One click delete lastUpdated (MacOS)

#Write your warehouse path hereREPOSITORY_PATH=~/.m2/repository echo LastUpdated find $REPOSITORY_PATH -name "* *" | xargs rm - fr echo search outCopy the code

The Windows script is as follows:

# Windows script @echo off
rem create by NettQun
Rem write down your warehouse path here
set REPOSITORY_PATH=D:\Java\maven-repository\maven-aliyun\repository
Rem is searching for...
for /f "delims=" %%i in ('dir /b /s "%REPOSITORY_PATH%\*lastUpdated*"') do (
    echo %%i
    del /s /q "%%i")Rem search completed
pause
Copy the code

Maven dependency conflict, reference error

The first two problems are mainly related to the local network, which is mainly caused by maven features.

Maven has A dependency passing feature. If A depends on B and B depends on C, then the dependency from C will be passed to A indirectly through B.

If there are multiple indirect dependencies that are different versions of each other, a dependency conflict can occur.

As shown above, if the application of A uses some new classes or methods added by E2.0, then A actually indirectly depends on E1.0 due to dependency conflict. As A result, these new classes or methods added by E cannot be found when IDEA is compiled.

The solution to this problem, is relatively simple, there is a conflict, we can directly solve the conflict.

You can install a Maven Helper plug-in in IDEA, open the POM file, click on the Dependency Analyzer option, and select the Conflicts button from there to see all the currently conflicting dependencies.

By selecting one of the dependencies, you can see the versions of all the conflicting dependencies on the right. Select one version and right-click Exclude.

Above we solve the problem by eliminating other indirect dependencies. Then there is another solution, which is to turn indirect dependence into direct dependence.

We can write the indirect dependency directly in the project POM file, so maven will use this dependency directly, and other indirect dependencies will be inactivated.

Maven indirectly relies on exceptions

This is a weird question, and this is the question that was mentioned at the beginning of the article.

The dependencies in the project are shown below.

Application A relies on B, which is an open source project, but the company makes some modifications to B internally. After the change, B depends on C, so A uses C to depend on some classes.

Since my local repository has downloaded the open source version of B, when I pull the source code of the A application locally, the dependency becomes the dotted line in the diagram.

IDEA is missing C dependencies, which results in a project compile error that cannot find some classes in C.

The solution to this problem is to find the B dependency in the local repository, and then delete and pull.

The solution is simple, but the key is to find this dependency in the middle.

IDEA Local cache exception

The last situation has much to do with IDEA.

IDEA is fast because when IDEA opens a new project, it builds the project index and generates the associated cache.

Sometimes, if the IDEA cache is broken, it can cause IDEA to fail to recognize Maven dependencies, which can cause IDEA to fail to find certain classes.

In this case, we need to delete the original cache and index and wait for IDEA to rebuild the cache and index.

Click File/Invalidate Cached in IDEA.

The last

If you’re faced with a new situation that doesn’t work out with any of the above solutions, use the ultimate solution.

Restart dafa, restart IDEA, restart computer