Question

In new Maven projects in IntelliJ, I see 2 ways of declaring new dependencies:

  1. Manually editing the pom.xml file, adding a <dependency/> declaration. This automatically (maven auto-import is on) adds Maven: ... entries to the project .iml file and all is well.
  2. Using the File -> Project Structure -> Dependencies menu. This only creates entries in the .iml file.

This seems like unwanted behavior. IntelliJ doesn't overwrite the .iml file when importing the pom.xml Maven file, but merges dependencies from the pom.xml and additionally-defined ones from the .iml.

I want to enforce a single way of adding dependencies in my team (using pom.xml only, for external mvn builds), and the ability to bypass the pom.xml and directly add dependencies to the .iml creates the illusion of a valid build (when in fact, it is not).

Am I missing something? How can I enforce one way of adding dependencies? Is there a way through which IntelliJ will add dependencies to the pom.xml file instead of the .iml files?

EDIT

In this question (IntelliJ IDEA + Maven what is the need for dependency entries in an iml file?) it is claimed that

This can be also used to experiment with dependencies without changing the pom.xml. Note that all the modifications you make will be reverted on next Maven import.

But this is not the behavior I see. The .iml file doesn't get reverted, but merged.

Was it helpful?

Solution

It sounds like you need a continuous integration environment.

This won't stop your team members adding dependencies in the wrong place, but it will ensure that is caught very early and flag it up that they need to add it to your pom, then it just comes down to education.

OTHER TIPS

Pom.xml is only to be used by Maven, IntelliJ does pick up the dependencies from the pom.xml if you have configured your IDE properly. If you however, want to use maven solely, you can add the jars to your local maven repo and then call them from your pom.xml normally via the <dependency> tag.

You can learn how to add local jars here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top