Java文件位于源代码根目录之外的情况(在IntelliJ中)。

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

Java file outside of source root intelliJ

问题

我已经从GitLab克隆了一个Spring Boot项目,在IntelliJ中有多个分支。我切换到我想要工作的分支。但是所有的文件都显示"Java文件在源代码根目录之外"的错误。请指导我做错了什么。
我刚刚安装了IntelliJ,这是我导入的第一个项目。

英文:

I have cloned a spring boot project from GitLab having multiple branches in IntelliJ. I checkout to the branch in which I want to work. But I am getting "Java file outside of source root" on all the files. Please suggest what I am doing wrong.
I just installed IntelliJ and it's the first project that I am importing.

答案1

得分: 127

如果您执行“从Git导入”操作,IntelliJ不会自动从Maven(或Gradle)导入项目结构。

一种后续操作是右键单击根目录中的pom.xml文件,然后选择“添加为Maven项目”。然后文件夹将被正确标记,并且相关的库将被导入。无需手动标记各个源文件夹。

我更喜欢在IntelliJ之外进行Git克隆,然后从本地文件系统在IntelliJ中打开项目,这样IntelliJ会自动导入Maven项目结构。

英文:

If you do an 'import from git', IntelliJ doesn't import the project structure from maven (or gradle) automatically.

One way to do this afterwards is to right-click on the pom.xml file in the root directory, and select 'Add as maven project'. Then the folders will be marked correctly, and dependent libraries will be imported. There should be no need to mark individual source folders manually.

I prefer to do a git clone outside of IntelliJ, and afterwards open the project in IntelliJ from the local filesystem, then IntelliJ imports the maven project structure automatically.

答案2

得分: 76

非常简单的解决方案:

在IntelliJ的右侧工具栏中点击“maven”,然后点击刷新按钮。

英文:

Very simple solution: -

Click on "maven" on the right toolbar in IntelliJ and then click on the refresh button.

答案3

得分: 62

我用以下方式修复了它:

文件 > 项目结构 > 模块 > 源文件

然后将Java文件目录标记为源文件。

英文:

I fixed it using this way:

File > Project Structure > Modules > Sources

and then marked java files dir as Sources.

答案4

得分: 34

这让我相当烦恼,我也是个新手。最终我通过以下方式解决了这个问题:

文件 > 项目结构 >

然后进入“模块”,使用最右边的 x 标记将根目录中的所有文件移除。然后使用 +添加内容根 来重新选择包含我的 .java 文件的 src 文件夹,然后点击应用(Apply)。最后,IntelliJ 将这些文件视为项目中的文件,文件夹在主视图中获得了蓝色方块。

英文:

This has been driving me quite mad and I am a newbie as well. I finally solved it by using:

file > project structure >

Then going to Modules and removing all the files from the root using the x marks on the far right. I then used the +Add Content Root to reselect the src folders that contained my .java and hit Apply. Finally InteliJ sees the files as in the project and the folders gain the blue square on the main view.

答案5

得分: 30

解决方法: "Java文件在源代码根目录之外"

  1. 打开项目结构。
  2. 在左侧窗格中的项目设置中选择 模块
  3. 点击包含您的Java文件(显示在源代码根目录之外的文件)的文件夹。
  4. 然后从 标记为: 选项中选择 源代码
  5. 对多个文件夹应用相同的步骤。

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

英文:

To Solve: "Java file outside of source root"

  1. Open Project Structure.
  2. Select Modules in Project Settings in left pane.
  3. Click on the folder in which your Java files (showing file outside of source root) are present.
  4. Then Select Sources from Mark as: option.
  5. Apply the same steps for multiple folders.

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

答案6

得分: 7

多模块项目。

选择根目录的pom.xml --> 点击Maven -- 解除关联 <-- 这将移除所有子模块

选择根目录的pom.xml --> 添加为Maven项目

然后一切正常...

英文:

MultiModule Project.

Select root pom.xml --> Click Maven -- Unlink <-- This will remove all sub modules

Select root pom.xml --> Add As Maven project

Then everythings worked..

答案7

得分: 6

右键单击 pom.xml 文件 >> maven >> 重新加载项目

英文:

Right click on pom.xm >> maven >> Reload project

答案8

得分: 5

已解决!!
转到您在IntelliJ中的项目 -> 右键单击pom.xml -> 添加为Maven项目。

英文:

Resolved !!
Go to your project on IntelliJ -> Right click on pom.xml -> Add as Maven Project.

答案9

得分: 5

右键单击pom.xml,然后点击“maven”,重新加载项目。

英文:

Right click on pom.xml then click on "maven" and reload project
Java文件位于源代码根目录之外的情况(在IntelliJ中)。

答案10

得分: 4

对于像我这样也看到 POM 文件被划掉并变灰(被忽略)的人:

你可以右键点击 POM.xml,选择 "Maven - Unignore projects"。

英文:

For ppl like me who also see that the POM file is crossed out and greyed(ignored):

You can right click the POM.xml, select "Maven - Unignore projects".

答案11

得分: 3

解决“IntelliJ中源代码根目录外的Java文件”问题

重新生成Maven源并更新文件夹

右键点击 pom.xml > Maven > 生成源代码并更新文件夹

项目现在应该可以运行了

这是菜单的示意图:

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

英文:

Solution to "Java file outside of source root intelliJ"

Regenerate Maven Sources and Update Folders

Right click the pom.xml > Maven > Generate Sources and Update Folders

The project should now run

Here's an image of what the menu looks like:

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

答案12

得分: 3

File -> 项目结构 -> 模块

删除整个模块,然后点击添加按钮,选择“导入模块”,选择您项目的根 Maven pom.xml。

这节省了我的时间。

英文:

File -> Project structure -> Modules

Remove the whole module, and click the add button then select 'Import Module', choose the root maven pom.xml of your project.

This saved my time.

答案13

得分: 2

在IntelliJ中:
点击Maven,
选择项目并右键,
选择取消忽略项目。

英文:

In intellij:
click Maven,
select the project and right,
select Unignore Projects

答案14

得分: 2

我从右上角的“Maven”(或“Gradle”)窗口执行了“Maven reload”(或“Gradle reload”),问题得以解决。

英文:

I did a 'Maven reload' (or Gradle reload) from the upper-right 'Maven' ('Gradle') window and problem is solved.

答案15

得分: 2

File
->Project Structure
-> Module
select the Sources tab in the third pane from the left

英文:

File
-&gt;Project Structure
-&gt; Module
select the Sources tab in the third pane from the left

答案16

得分: 1

转到

文件 >> 项目结构 >> 模块 >> 导入模块 >> (此时,您的 src 文件夹将自动显示在右侧窗格中) >> 应用 >> 确定
英文:

Go to

File &gt;&gt;Project Structure&gt;&gt;Modules&gt;&gt; Import Module&gt;&gt;  (At this point your src folder will appear on the right most pane automatically ) &gt;&gt; Apply &gt;&gt; OK

答案17

得分: 1

IntelliJ可能会忽略多模块项目中的文件。被忽略的模块将使IDE难以找到该文件的根源。因此,java文件位于源代码根目录之外

另一个解决方案:

首选项 -&gt; 构建、执行、部署 -&gt; 构建工具 -&gt; Maven -&gt; 被忽略的文件

现在,取消选中您不希望IntelliJ忽略的模块。

英文:

IntelliJ may be ignoring files from a multi-module project. Modules that are ignored will make it harder for the IDE to find the root of that file. Hence, java file outside of source root

Another solution:

Preferences -&gt; Build, Execution, Deployment -&gt; Build Tools -&gt; Maven -&gt; Ignored Files

Now, uncheck the modules you do not want IntelliJ to ignore.

答案18

得分: 1

我在Android Studio Chipmunk 2021.2.1 Patch 1中的一个Android项目中遇到了类似的问题。看起来与使用manifest.srcFile相关的已知错误有关:https://issuetracker.google.com/issues/218370114?hl=ko

显然,解决方案是切换到canary通道或等待修复在稳定通道发布。或者在你的build.gradle中去掉manifest.srcFile

英文:

I'm having a similar issue for an Android project in Android Studio Chipmunk 2021.2.1 Patch 1. It looks like a known bug related to using manifest.srcFile: https://issuetracker.google.com/issues/218370114?hl=ko

Apparently the options are switching to the canary channel or waiting for the fix to be released on the stable channel. Or getting rid of manifest.srcFile in your build.gradle.

答案19

得分: 1

前往Intelij

文件 > 项目结构 > 模块 > 添加

添加模块

英文:

Go to Intelij

File &gt; Project Structure &gt; Modules &gt; Add

add module

答案20

得分: 1

在我的情况下,我忽略了一些项目,因此 pom.xml 文件也显示为禁用。因此,我按照以下步骤操作:

右键点击那些 pom.xml 文件 -> Maven -> 取消忽略项目

英文:

In my case, I have ignored some projects and hence the pom.xml files also showed disabled. Hence I followed the below steps.

>right-clicked those pom.xml files -> Maven -> Unignore Projects

答案21

得分: 1

我尝试了以上的方法。最终发现这是因为不兼容的Maven版本引起的问题。在IntelliJ中修复设置,将其指向较低版本的Maven,然后将其添加为Maven项目,问题得以解决。

英文:

I tried all of the above. Finally figured out this is because of an issue with incompatible Maven version. Fixing the settings in IntelliJ to refer to a lower version of Maven and then Add as Maven project helped.

答案22

得分: 1

我在我的多模块Maven项目中遇到了相同的问题。执行了这个问题下面提到的一些步骤解决了问题,但并不是永久性的。问题每天都会出现。为了解决这个问题,我不得不进入项目设置,如下所示。

然后,我勾选了以下复选框。

我不知道这是否会解决每次出现的问题。但对我来说,这是解决方法。

  • Maven版本 - 3.9.1
  • IntelliJ版本 - 2023.1.2(社区版)

我分享这个希望能帮助到某人。谢谢!

英文:

I had the same problem in my multi-module maven project. Doing some steps mentioned under this question solved the issues but it was not permanent. The issue appeared everyday. In-order to fix this I had to go to the project settings as shown below.

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

After that, I did check the following check boxes.

Java文件位于源代码根目录之外的情况(在IntelliJ中)。

I don't know whether this would solve the issue for every occurrence. However, for me this was the fix.

  • Maven version - 3.9.1
  • IntelliJ version - 2023.1.2 (Community Edition)

I am sharing this thinking this might help someone. Thank you!

答案23

得分: 0

我只会返回翻译好的部分,不包括代码部分:

如果您的项目中有多个 pom 文件,在项目的右侧 Maven 侧边栏中,点击加号('+')按钮,然后导航到另一个没有在项目结构(左侧侧边栏)中的 pom 文件旁边带有 Maven('M')图标的 pom 文件。

然后点击 'OK' - 这会从未使用的 pom 文件中加载所有的 Maven 依赖项,对我来说,这样终于解决了所有问题!

Maven 侧边栏 > 加号图标(+) > 导航到其他 pom 文件 > 'ok'。

英文:

I'm just gonna throw my solution to this problem in here, since none of the above worked:

If there are multiple pom files in your project, on the Maven sidebar to the right, click the plus ('+') button and navigate to the other pom file that does not have the Maven ('M') icon next to it in the project structure of your project (left sidebar).

Then click 'OK' - this then loads all the Maven dependencies from the unused pom file and, for me, everything was finally fixed!

Maven sidebar > plus icon (+) > direct to other pom file > 'ok'

答案24

得分: 0

我不得不将我所工作的模块打开到一个新窗口中来解决那个问题。

英文:

I had to open the module I worked on to a new window to solve that.

答案25

得分: 0

很讨厌再添加另一种方法;但在尝试了上述方法失败后,这个方法对我起作用了。
我有一个多模块项目。
在Maven工作区取消所有Maven项目的链接,然后通过在项目工作区中点击根poom.xml重新加载为Maven项目。
不确定这是否总是有效,适用于每个人。

英文:

Hate to add yet another method; but this one worked for me after trying and failing with the above methods.
I had a multi-mode project.
Unlinked all maven projects in Maven workspace and reloaded as maven project by clicking root poom.xml in project workspace.
Not sure whether this will always work and for everyone.

答案26

得分: 0

我在我的多模块 maven 项目的一个模块中遇到了同样的问题。在我的 InteliJ IDEA 中,我之前安装了 Maven Helper 插件。我在该模块上执行了右键点击,然后在上下文菜单中点击 Run Maven,然后点击 Reimport。执行这些操作后,我的问题得到了解决。

英文:

I faced the same problem in one of the modules in my multi module maven project. In my InteliJ idea, I've installed Maven Helper plugin before. I did right click on the module and within that contextual menu click Run Maven and then Reimport. After doing that, my issue resolved.

答案27

得分: 0

对我有效的解决方案是右键单击src文件夹,然后选择“标记目录为” -> “Sources Root”或“Test Sources Root”。这也在问题的评论中提到过。但是我之前没有注意到,所以想把它发布为一个答案。

英文:

The solution that worked for me was to right click on the src folder and select "Mark Directory as" -> "Sources Root" or "Test Sources Root".

This is also mentioned in the comment to the question. But I had missed it so wanted to post it as an answer.

答案28

得分: 0

仅适用于那些有类似小错误的人:

我遇到的简单问题和解决方法是进入我克隆下来的确切文件中。我不小心使用了 idea . 快捷方式打开了父目录,而不是我需要工作的项目。

英文:

Just for anyone who has the minor mistake that I had:

My simple issue and fix was to cd into the exact file I cloned down. I accidentally shortcut opened the parent directory with idea . instead of the project I needed to work on.

答案29

得分: 0

我不得不在IntelliJ中更改Maven路径。 首选项-> 构建、执行、部署-> 构建工具-> Maven。

使用捆绑的Maven 3路径解决了这个问题,Spring Boot应用程序启动正常。

英文:

I had to change the Maven path in IntelliJ. Preferences-> Build, Execution, Deployment-> Build Tools-> Maven.

Using the bundled Maven 3 path solved this and the Spring Boot application started up just fine.

答案30

得分: 0

请执行以下两步:

步骤1:右键单击根目录下的pom.xml文件,选择Maven->生成源代码并更新文件夹。

步骤2:右键单击根项目级别的pom.xml文件,然后选择Maven->重新加载项目。

英文:

Please do the below 2 steps:

Step-1: Right click on root pom.xml file Maven-> Generate Sources and update folders

Step-2: Right click on root project level pom.xml >> maven >> Reload project

答案31

得分: 0

对于一个项目,其中 build.gradle 配置了各种自定义的构建变体,我尝试了上面提到的所有方法,但仍然不起作用。最终,当我从 Android Studio 的左侧面板选择了正确的构建变体时,它就起作用了。

英文:

For one project where build.gradle was configured for various custom build variants, I tried everything mentioned above but still it didn't work. Finally when I chose correct build variant from left panel of Android Studio, it just worked.

答案32

得分: 0

这是对我有效的解决方法:

  1. 在IntelliJ中关闭项目
  2. 删除项目目录中的.idea文件夹
  3. 在IntelliJ中重新打开项目
英文:

This is the solution that worked for me:

  1. Close the project in IntelliJ
  2. Delete the .idea folder in the project directory
  3. Reopen the project in IntelliJ

答案33

得分: 0

然而,已经提供了许多选项,我从上面尝试了2-3个,但效果不如预期。因为Intellij Idea将所有的工作空间设置都保存在项目根目录下的.idea文件夹中,*.iml文件位于所有子项目下,用于链接模块。
要清除所有设置 - 要么关闭IDEA,要么让其空闲一段时间,然后从终端执行以下命令 - 假设我的根项目是"ewfsa" -

cd ewfsa
rm -rf .idea
rm -rf **/*/*.iml
rm -f *.iml 

然后选择"从磁盘重新加载"选项,如果IDEA已关闭,则打开IDEA。
每个项目都会重新加载,所有丢失的设置都会恢复。

英文:

However there are many options already given, i tried 2-3 from above but it did not work as expected.Because, Intellij Idea keeps all workspace setting in .idea folder on root level of project and *.iml file under all sub-project to link the modules.
To clean all the settings - either close IDEA or left idle for some time and from back using terminal executed below command - assume my root project is "ewfsa" -

cd ewfsa
rm -rf .idea
rm -rf **/*/*.iml
rm -f *.iml 

and then select option "reload from disk" and if IDEA closed then open IDEA.
every project reloaded and all missing setting restored.

huangapple
  • 本文由 发表于 2020年8月21日 18:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521181.html
匿名

发表评论

匿名网友

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

确定