Reputation: 51
I just installed IntelliJ Community 2020.2.1 (Maven 3.6.3). I have a relatively simple project named "genetest1", with an aggregator/parent pom and two submodules, core and playground:
genetest1
pom.xml (aggregator/parent)
core
pom.xml
src\main\java\com\gene\genetest1\core
\util
JsonIO.java
playground
pom.xml
src\test\java\com\gene\genetest1\playground
\json
JacksonBaseTest.java
resources
\suiteFiles
all.xml
default.xml
The relevant pom sections are:
genetest1:
<modelVersion>4.0.0</modelVersion>
<groupId>com.gene</groupId>
<artifactId>genetest1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>playground</module>
</modules>
...
core:
<modelVersion>4.0.0</modelVersion>
<artifactId>genetest1-core</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.gene</groupId>
<artifactId>genetest1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
...
playground:
<modelVersion>4.0.0</modelVersion>
<artifactId>genetest1-playground</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.gene</groupId>
<artifactId>genetest1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
...
The idea is to have TestNG tests in the playground module, and during their execution call utility methods in the core module. In the above structure, the playground JacksonBaseTest.java class contains a TestNG test (@Test), imports the core JsonIO.java class, and the test calls a method in the JsonIO.java class.
After importing the project into IntelliJ via the root pom, the core src\main\java folder is correctly marked as Sources root (blue), and the playground src\test\java folder is correctly marked as Test sources root (green):
I then manually set the dependency of the playground module on core:
Without the above dependency setting, the import statement in playground JacksonBaseTest.java:
import com.gene.genetest1.core.util.JsonIO;
is called out as an error "cannot resolve symbol JsonIO". When I set the dependency, the error goes away. Further, after setting the dependency and performing Analyze > Module Dependencies..., it shows that Intellij knows about this dependency:
Compiling the project from IntelliJ with "mvn test" successfully compiles the core module, but throws an error compiling the playground module:
package com.gene.genetest1.core.util does not exist
where the package clearly exists, and Intellij evidently knows that it exists. Not sure what I'm doing wrong. I tried the suggested File > Invalidate Caches > Invalidate and Restart, no change. I also found my File > Settings > Build, Execution, Deployment > Build Tools > Maven > Importing > JDK for Importer defaulted to internal Java 11, so I changed it to Java version 14 (installed on my machine and specified in my root pom), no effect. Most of the posts I found on this issue involved dependencies on External Libraries, but here the dependency is not on an external library.
This project is intended to compile the submodules and run the TestNG tests, without saving any artifacts, hence the maven execution is only through the test phase.
Upvotes: 1
Views: 1548
Reputation: 16391
I then manually set the dependency of the playground module on core:
In case of the Maven(Gradle/SBT) based IDE projects you never set the dependencies in IDE manually. You should configure all dependencies and project structure in Maven(Gradle/SBT) instead. You need to add a dependency on the core module in playground module pom.xml. Refer to Maven documentation about building multi-module projects.
Upvotes: 1