英文:
Why does IntelliJ recognize a module dependency but still throw a "package does not exist" compilation error?
问题
我刚刚安装了IntelliJ Community 2020.2.1(Maven 3.6.3)。我有一个相对简单的项目,名为"genetest1",它有一个聚合器/父POM和两个子模块,core和playground:
genetest1
pom.xml(聚合器/父)
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
相关的POM部分如下:
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>
...
这个项目的想法是在playground模块中有TestNG测试,在它们的执行过程中调用core模块中的实用方法。在上述结构中,playground的JacksonBaseTest.java类包含一个TestNG测试(@Test),导入了core的JsonIO.java类,测试调用了JsonIO.java类中的一个方法。
在通过根POM将项目导入IntelliJ后,core的src\main\java文件夹正确地标记为Sources root(蓝色),而playground的src\test\java文件夹正确地标记为Test sources root(绿色):
然后我手动设置了playground模块对core模块的依赖:
- 打开文件 > 项目结构... > 项目设置 > 模块
- 高亮显示模块genetest1-playground
- 在右侧选择依赖项
- 点击右侧的+号,选择模块依赖...
- 选择genetest1-core并点击确定
- 点击应用和确定
如果没有上述的依赖设置,在playground的JacksonBaseTest.java文件中的import语句:
import com.gene.genetest1.core.util.JsonIO;
会被标记为错误,显示"无法解析符号JsonIO"。当我设置了依赖关系后,错误消失了。此外,在设置了依赖关系并执行了分析 > 模块依赖项...之后,IntelliJ显示知道这个依赖关系:
从IntelliJ中使用"mvn test"编译项目可以成功编译core模块,但在编译playground模块时会出错:
package com.gene.genetest1.core.util does not exist
其中包明显存在,而且IntelliJ显然知道它存在。不确定我做错了什么。我尝试了建议的 文件 > 无效化缓存并重启,没有变化。我还发现文件 > 设置 > 构建、执行、部署 > 构建工具 > Maven > 导入 > 导入器的JDK默认为内部Java 11,所以我将其更改为Java版本14(已安装在我的计算机上,并在根POM中指定),但没有效果。我在这个问题上找到的大部分帖子都涉及对外部库的依赖,但这里的依赖关系不是外部库。
这个项目旨在编译子模块并运行TestNG测试,而不保存任何构件,因此Maven的执行只在测试阶段进行。
英文:
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:
- Open File > Project Structure... > Project Settings > Modules
- Highlight module genetest1-playground
- Select Dependencies on the right
- Click + on the right and select Module Dependency...
- Select genetest1-core and click OK
- Click Apply and OK
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.
答案1
得分: 1
我随后手动设置了playground模块对core模块的依赖:
对于基于Maven(Gradle/SBT)的IDE项目,您不需要在IDE中手动设置依赖关系。您应该在Maven(Gradle/SBT)中配置所有依赖关系和项目结构。您需要在playground模块的pom.xml中添加对core模块的依赖。有关构建多模块项目的信息,请参阅Maven文档。
英文:
>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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论