How does IntelliJ's "Reimport All Maven Projects" button work?

18,193

Solution 1

I think you miss the part with local and remote repository.

If you run mvn -U it forces maven to download all libraries from remote repository that will be your company nexus or maven repo. The main difference with -U and without is that -U will override your local SNAPSHOT jars with remote SNAPSHOT jars. The local SNAPSHOT jars came from install and remote came from deploy command.

There will come the confusion with reimport. Reimport will load your local jars instead of remote jars, especially SNAPSHOT ones. You can enable force update snapshot in maven menu, which will enable -U switch in intellij.

Also double check that you share same .m2 directory with your system maven and intellij maven. You can see that in user setting file and compare it with command line maven. Just run mvn -X and check the settings section.

[DEBUG] Reading global settings from /usr/local/Cellar/maven/3.3.9/libexec/conf/settings.xml
[DEBUG] Reading user settings from /Users/xbaran/.m2/settings.xml
[DEBUG] Reading global toolchains from /usr/local/Cellar/maven/3.3.9/libexec/conf/toolchains.xml
[DEBUG] Reading user toolchains from /Users/xbaran/.m2/toolchains.xml
[DEBUG] Using local repository at /Users/xbaran/.m2/repository

enter image description here


asker's note for future readers

This answer did not fully explain the reimport button's behaviour, so I'm going to add my observations here for posterity.

There seems to be something going on with IntelliJ's caches (i.e., the .idea folder). using mvn clean install -U in terminal works as described here, but does not affect IntelliJ IDE; missing symbols are still described as missing. However, if I press the "magic reimport button", those missing symbols successfully resolve.

I can only assume that the reimport button is basically instructing IntelliJ to refresh its own cache, which is why there is a difference in behaviour.

Solution 2

If you changed the pom.xml file, IDEA needs to update the project structure. For example if you've added there some more dependencies, IDEA needs to add them as project libraries.

So "Maven > Reimport" is used exactly to that - to reimport a maven module. It does not trigger any maven commands with any arguments.

"Reimport All" does the same but for all maven modules in the project.

As a side note, instead of using those two actions, in "Settings > Build, Execution, Deployment > Build Tools > Maven > Importing" you can choose "Import Maven projects automatically". This will automatically invoke "Reimport" action when the pom.xml is changed.

Share:
18,193
Johnny
Author by

Johnny

Eternal Student.

Updated on June 08, 2022

Comments

  • Johnny
    Johnny about 2 years

    How does IntelliJ's "Reimport All Maven Projects" button actually work?

    I'm asking this because I see that IntelliJ's reimport button's behavior is different than that of running the mvn command with the -U argument, and it's not clear to me why.

  • Johnny
    Johnny almost 9 years
    Thanks, this part was actually clear to me. The part is the unclear part to me is, for example: in projectA pom.xml we have dependency in projectB, when reimport all button clicked (we working in projectA), also updates the files of dependency. But, sometimes not. Why is that happen?
  • Monarch Wadia
    Monarch Wadia almost 8 years
    Did you ever find an answer for this @StasS ? I'm curious also.
  • Johnny
    Johnny almost 8 years
    Still looking @monarch, maybe the bounty will help :)
  • Johnny
    Johnny almost 8 years
    Thanks. Are you sure about the 'Reimport will load your local jars instead of remote jars' part? I thought that the install goal responsible for adding the artifacts to local repository.
  • Milan Baran
    Milan Baran almost 8 years
    Install goal adds jars to your local repository and deploy adds them to remote repo if it is provided. Well, but we are talking about jars import or loading. The default strategy is 1. look into local repo and if missing 2. look into remote repo 3. Download it to local repo. First import hit all three strages but second import will hit just first stage, because jars are already downlaoded in your local repo. With -U flag first step is skipped. In case of IntelliJ without that flag checked (default is unchecked) the -U flag is not used in reimport or any mvn command run from mvn panel.
  • Monarch Wadia
    Monarch Wadia almost 8 years
    thank you for your answer. i had a question: lets say i have a project Y that depends on X. i first make changes to X. then, i can do cd Y; mvn clean install -U in the terminal successfully; the build succeeds in the terminal; but i'm still seeing missing symbols in intellij unless i press the reimport button. what gives in this situation? does the reimport button hook into Intellij's cache system?
  • Monarch Wadia
    Monarch Wadia over 7 years
    the answer did not fully explain the behaviour, but led me on the right path. I'm going to award this answer the bounty, but i have added my own observations (pending peer review)
  • Johnny
    Johnny over 7 years
    Thanks MilanBaran and monarch, that really helped