Cross maven module IntelliJ dependencies

Hello IntelliJ Community:

Is there a way to resolve dependencies for a maven project from both the maven dependencies and other IntelliJ modules in the same project. I see in the dependencies tab of the maven module project settings that I can add dependencies on other modules, but for some reason, after adding the dependency; the source still marks the classes as not found. The only way I can resolve the classes in the editor is if I mvn install the the dependent module's artifact to my local maven repo and update my pom to include the dependent artifact.

For example, I can't get the following to work:

1) Create IntelliJ project
2) Import module A from existing pom (standard java maven project)
3) Import module B from existing pom (standard java maven project)
4) Add module B as a IntelliJ module dependency (under project settings) to module A (module A's pom does not include a dependency on module B, but module A's source classes make use of classes in module B)
5) Classes from module B used in module A can not be resolved and are marked as an error, the only way for IntelliJ to be happy is if I mvn install module B and then add a dependency on module B into module A's pom. Is there no way to avoid this installation step and have IntelliJ simply see all source files in the same project?

I know this doesn't really follow the grain of maven, but it would still be useful for me, because there are times when intelliJ can't resolve maven dependencies for some reason or another, yet the source is already loaded in the project as another module.

Thanks for the help!

5 comments

I am facing the same problem. Would be great if someone can provide an workaround or if intellij idea takes care of it. Commenting just to convey that there are more users looking for the exact same feature.

Thanks,
Arup

0

I haven't experienced the problem firsthand, so my only guess is that IDEA goes overzealous with POM<->project synchronization, but the 'propper' way to set up a project like yours would be to add a SNAPSHOT dependency from A to B and create an aggregation POM, including both A and B.

Then you do not need to do any magic - on import IDEA will link both modules and the code changes will take effect without having to deploy to local repo.

0

you just need to create a pom that links together your sub projects (packaging=pom), and import that one instead of the individual projects.

In fact, IDEA jsut does exactly the same thing as maven here : unless you create a multimodule pom, all your dependencies are resolved using the repo, and not "locally" from your project

0

I think IntelliJ IDEA should provide a feature to allow to depend on another maven module/project so if you are working on project A that depends on project B, any changes to project B gets instantly refreshed for project A.

The alternative you have right now is the following, create a aggregator pom as the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <version>1.0</version>
  <artifactId>projectsaggregator</artifactId>
  <packaging>pom</packaging>

   <modules>
       <module>../projectA</module>
       <module>../../projectB</module>
   </modules>



</project>


The open this new project as the main project

0

Resurrecting very old thread I found via Google, as I found a way to avoid creating an aggregator POM.  I'm new to IntelliJ - long time Eclipse user.  I have a similar scenario to that described by OP.  My module A actually has a POM dependency on a particular version of module B, but I wanted to use both modules pulled from Git repositories.  Since the projects are developed independently, I didn't want to create an aggregator POM. Eclipse makes this very easy, by allowing you to specify that one Eclipse project depends on another Eclipse project.

Here's what worked for me in IntelliJ Community 2018.1 (after trying other approaches that didn't work):

  1. Import the independent POM module B into IntelliJ as a new project.  Build to ensure no issues.
  2. In dependent module A's POM file, edit the dependency on module B so that it has the same version as the one you imported in step 1.
  3. In the existing project, import the dependent module A.  I was able to now build module A (or entire project), and everything resolved without issue.  Classes in module A can reference classes in module B without IntelliJ complaining.  And I can run JUnit tests in module A from within IntelliJ by right-clicking the unit test and selecting Run from the context menu.

With this approach, I avoided creating an aggregator POM.  And, I can update both modules by pulling updates from the origin Git repos, import the changes into IntelliJ, and just rebuild everything without having to fix anything.  If you make module A dependent on module B from within IntelliJ (instead of from A's POM), you lose that dependency each time you refresh from the file system.

Hope this helps others in a similar situation.

0

Please sign in to leave a comment.