为什么IntelliJ识别了模块依赖,但仍然抛出“包不存在”编译错误?

huangapple go评论74阅读模式
英文:

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(绿色):

为什么IntelliJ识别了模块依赖,但仍然抛出“包不存在”编译错误?

然后我手动设置了playground模块对core模块的依赖:

  • 打开文件 > 项目结构... > 项目设置 > 模块
  • 高亮显示模块genetest1-playground
  • 在右侧选择依赖项
  • 点击右侧的+号,选择模块依赖...
  • 选择genetest1-core并点击确定
  • 点击应用和确定

如果没有上述的依赖设置,在playground的JacksonBaseTest.java文件中的import语句:

import com.gene.genetest1.core.util.JsonIO;

会被标记为错误,显示"无法解析符号JsonIO"。当我设置了依赖关系后,错误消失了。此外,在设置了依赖关系并执行了分析 > 模块依赖项...之后,IntelliJ显示知道这个依赖关系:

为什么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:

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;groupId&gt;com.gene&lt;/groupId&gt;
	&lt;artifactId&gt;genetest1&lt;/artifactId&gt;
	&lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
	&lt;packaging&gt;pom&lt;/packaging&gt;
	
	&lt;modules&gt;
		&lt;module&gt;core&lt;/module&gt;
		&lt;module&gt;playground&lt;/module&gt;
	&lt;/modules&gt;
	
	...

core:

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;artifactId&gt;genetest1-core&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    
    &lt;parent&gt;
    	&lt;groupId&gt;com.gene&lt;/groupId&gt;
    	&lt;artifactId&gt;genetest1&lt;/artifactId&gt;
    	&lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;
    
    ...

playground:

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;artifactId&gt;genetest1-playground&lt;/artifactId&gt;
	&lt;packaging&gt;jar&lt;/packaging&gt;
	
	&lt;parent&gt;
		&lt;groupId&gt;com.gene&lt;/groupId&gt;
		&lt;artifactId&gt;genetest1&lt;/artifactId&gt;
		&lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
	&lt;/parent&gt;
	
	...

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):

为什么IntelliJ识别了模块依赖,但仍然抛出“包不存在”编译错误?

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:

为什么IntelliJ识别了模块依赖,但仍然抛出“包不存在”编译错误?

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.

huangapple
  • 本文由 发表于 2020年9月19日 04:57:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63962750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定