IntelliJ:源根未正确处理 [英] IntelliJ: Source root is not handled correctly

查看:383
本文介绍了IntelliJ:源根未正确处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下项目结构:

ModuleName (=Content Root)
  |---src
  |    |---com
  |         |---company
  |             |---file1.java
  |             |---file2.java
  |---test-src
  |    |---com
  |         |---company
  |             |---test.java
  |--- .classpath
  |--- .project

这最初是一个Eclipse项目,我需要将其导入IntelliJ.

This was an Eclipse project initially, and I need to import it in IntelliJ.

但是,每次尝试导入时,IntelliJ都与源根文件夹完全混淆了.

However, everytime I try to import it, IntelliJ is completely confused with the source root folder.

在查看Java源文件时,在程序包声明中出现以下错误:

Looking at the java source files, I get the following error in the package declaration:

package com.company;

错误:程序包名称'com.company'与文件路径'src/com.company'不对应

Error: Package name 'com.company' does not correspond to the file path 'src/com.company'

错误:程序包名称'com.company'与文件路径'test-src/com.company'不对应

Error: Package name 'com.company' does not correspond to the file path 'test-src/com.company'

我在IntelliJ中正确地将'src'和'test-src'文件夹标记为源根.但是,它始终认为这些文件夹是程序包层次结构的一部分.

I correctly marked the 'src' and 'test-src' folders as source-roots in IntelliJ. However, it always thinks that these folders are part of the package hierarchy.

我在大约30个不同的模块中的3个中有此问题.所有模块的结构都相同,我几乎看不到IntelliJ对大多数模块起作用的任何原因,但对这三个模块却无效.

I have this problem in 3 of about 30 different modules. All modules are structured the same, and I don't really see any reason why IntelliJ works for most of them, but fails for those three.

我该如何解决?

推荐答案

为完整性起见-如果有人使用相似的设置遇到相同的问题,以下是解决方法:

For completeness' sake - if anyone runs into the same problem with a similar setup, here is the solution:

另一个模块(我们称其为模块B)对模块A生成的.jar文件有依赖性.这通常工作得很好,可以在模块B的lib文件夹中找到所需的.jar.问题在于模块B的.classpath文件(用于将模块导入IntelliJ中)包括以下条目(我认为它是由eclipse在较早的时候生成的):

Another module (let's call it module B) had a dependency on the .jar file generated by module A. This normally works perfectly fine, and the required .jar can be found in module B's lib folder. The issue was that module B's .classpath file (which was used to import the modules into IntelliJ) included the following entry (I assume it was generated by eclipse at some earlier point):

<classpathentry kind="lib" path="lib/ModuleA.jar" sourcepath="/ModuleA/" />

现在,似乎IntelliJ对此源路径条目(由于它未指向有效位置)感到极为困惑,并且以某种方式影响了模块A的包装结构. 有趣的是(该错误使调试和发现实际问题变得非常痛苦)是该错误并未在有问题的.classpath文件所在的模块B中显示出来(因为IntelliJ不会显示警告从.classpath文件导入的源无效),而是在父模块A中.

Now it seems like IntelliJ became extremely confused by this sourcepath entry (as it doesn't point to a valid location) and this somehow affected module A's packaging structure. What's interesting about this (and made it a huge pain to debug and find the actual issue) is that the error didn't manifest itself in module B where the problematic .classpath file was located (because IntelliJ doesn't show a warning that the sources imported from the .classpath file were invalid), but rather in the parent module A.

删除源路径条目(或将其指向有效位置,即lib/ModuleA.jar)解决了该问题.

Removing the sourcepath entry (or pointing it to a valid location, i.e.: lib/ModuleA.jar) fixed the problem.

这篇关于IntelliJ:源根未正确处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆