1

I have downloaded a simple project from a Git repository and opened it in Intellij. A couple of the import statements are highlighted in red, stating "Cannot resolve symbol." The packages that cannot be resolved are: zaxxer, springframework.

Normally in Perl or Python I would fire up a dependency manager like CPAN and simply install them, but my understanding is that there isn't really any such thing in Java and that dependencies are resolved sort of on a project-by-project basis.

I have Maven 3 installed, and my project has a pom.xml file, for what it's worth. What should I do now?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Stephen
  • 8,508
  • 12
  • 56
  • 96
  • 2
    File - Open... - choose the pom.xml. And IntelliJ should create the project and download all the dependencies. Maven **is** a dependency manager. Execute `mvn install` in the command line, and all dependencies should be downloaded, the project built and tested, and the generated artifact stored in your local repo. Why don't you just read about Maven? – JB Nizet Mar 13 '18 at 17:43
  • Yes, if you have Maven it should install all dependencies, but you have to actually "fire it up" first. – markspace Mar 13 '18 at 17:45
  • @JBNizet thanks, I will read about Maven. I just wasn't actually sure if I that's what I needed to be relying on to get this working. As for File->Open->choose the pom.xml... the way I originally opened the project was by opening the containing folder. I just did it again by selecting the pom.xml, but either way it doesn't seem to be automatically downloading anything for me. I can run `mvn install` as you said, but I was under the impression from a separate question that IntelliJ would somehow do this automatically. – Stephen Mar 13 '18 at 18:05

1 Answers1

1

If you have opened the project in IntelliJ you can right click the module and choose "Add framework support". Choose Maven there, if it isn't there then it is already enabled.

Then, simply open your pom file. When indexed there should appear an dialog in the lower right corner asking you if you want to auto import changes or just import them manually. Click on one of them and the dependencies will be added.

If you can't see this notification it may have already been shown, then simply press on the "Event log" in the bottom right corner and the notification should be displayed (with eventual other info).

Edit from OP: Solution was: 1) Turn on the view in View > Tool Windows > Maven Projects. There was no Maven project here, so I added it (selected the pom.xml). 2) As described in an answer at Failed to Read Artifact Descriptor: IntelliJ "In Intellij, go to File -> Settings -> Build, Execution, Deployments -> Maven Check the box, ‘Always update snapshots’

It's a little weird that "always update snapshots" isn't turned on by default for IntelliJ, but this got things working.

Stephen
  • 8,508
  • 12
  • 56
  • 96
Osyx
  • 26
  • 4
  • Looks like it is already enabled for me. I saw the notification you mentioned earlier, and I clicked on the option to import once (not to turn on auto-import). However, nothing has been done - the import statements are still not resolved. // I can open the Event Log you mentioned, but it is just text - it does not give an option to recreate that dialogue so I can try again. – Stephen Mar 13 '18 at 18:21
  • You can enable auto-import if you go to **File > Settings > Build, Execution, Deployment > Build Tools > Maven > Importing**. If it still doesn't do anything after enabled, you can try to simply remove a symbol (I usually just change a version number) in the dependencies and re-add it once again. That should force IntelliJ to reread the file. – Osyx Mar 13 '18 at 18:37
  • I checked "Import Maven Projects Automatically" in the dialogue you mentioned, and removed and re-added the version tag (I didn't save or anything but I think that files are saved automatically in IntelliJ). Still nothing. Note that I haven't actually run any `mvn` commands from the command line - I'm still unsure if I need to do that, or if IntelliJ will do that for me. – Stephen Mar 13 '18 at 18:58
  • 1
    Indeed they are. You shouldn't need to run any maven commands (or install maven at all by yourself) when it's working in IntelliJ. If you go to **View > Tool Windows > Maven Projects** are there any errors displayed there? – Osyx Mar 13 '18 at 19:14
  • OK, I was able to solve it with your help. I'm going to add some details to the answer and then accept it. – Stephen Mar 13 '18 at 20:24
  • I'm glad that I was able to help, great that you managed to solve it! – Osyx Mar 14 '18 at 01:38