Java IntelliJ IDE | .iml 文件丢失或删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36542288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:58:42  来源:igfitidea点击:

IntelliJ IDE | .iml File lost or deleted

javatomcatintellij-ideaproject-structure

提问by WetWer

Last week I was working on a Java EE project with IntelliJ IDE.

上周我正在使用 IntelliJ IDE 进行 Java EE 项目。

Today: As I started up my IntelliJ 2016.1 it gave me the error:

今天:当我启动 IntelliJ 2016.1 时,它给了我错误:

Error Loading Project

加载项目时出错

Cannot load module FirstWebApp

无法加载模块 FirstWebApp

As I looked at the details it said it couldnot find my FirstWebApp.iml file and so could not load my project.

当我查看详细信息时,它说它找不到我的 FirstWebApp.iml 文件,因此无法加载我的项目。

I checked out my project structure in the Explorer and everything except the iml file were there. I also looked at the Recycle Bin but I couldn't find it there aswell.

我在资源管理器中检查了我的项目结构,除了 iml 文件之外的所有内容都在那里。我也看了回收站,但我也找不到它。

  • How can such a file just disapear?

  • How can I get my iml file back if its possible?

  • What do these iml files do?

  • 这样的文件怎么会消失呢?

  • 如果可能,我怎样才能取回我的 iml 文件?

  • 这些 iml 文件有什么作用?

I'm new in the Java World. If more information is needed just comment! :)

我是 Java 世界的新手。如果需要更多信息,请发表评论!:)

采纳答案by dambros

*.iml files have nothing to do with your code/Java. It is a hidden file created by Intellij on the root folder of your project which contains your module information. This is the reason you should not version this kind of file, afterall other developers using a different IDE won't need them.

*.iml 文件与您的代码/Java 无关。它是 Intellij 在项目根文件夹中创建的隐藏文件,其中包含您的模块信息。这就是您不应该对此类文件进行版本控制的原因,毕竟使用不同 IDE 的其他开发人员不需要它们。

If you closed Intellij, I think there isn't a way to get the same file back, but on the other hand it is pretty simple to regenerate a new .iml file:

如果您关闭了 Intellij,我认为没有办法恢复相同的文件,但另一方面,重新生成新的 .iml 文件非常简单:

Gradle based project

基于 Gradle 的项目

Click the Sync Project with Gradle Filesbutton (this one enter image description here) in the toolbar;

点击工具栏中的Sync Project with Gradle Files按钮(这个在此处输入图片说明);

Maven based project

基于Maven的项目

Go to File > Project Structures > Modules > Add > Import Module > (select your modules base directory) > OK

转到文件 > 项目结构 > 模块 > 添加 > 导入模块 >(选择您的模块基本目录)> OK

Edit:

编辑:

My previous answer were based on IntelliJ 14 and it seems some things have changed on the newer versions, as pointed by hBrentand BeCon the comments.

我之前的答案是基于 IntelliJ 14 的,正如hBrentBeC在评论中指出的那样,似乎在较新版本上有些事情发生了变化。

Using Intellij Community 2018.1 for this update:

使用 Intellij Community 2018.1 进行此更新:

Maven based project

基于Maven的项目

The procedure remains the same, the only difference I saw is the Addicon isn't green anymore. One can also try removing the rootFolder/.idea/modules.xmland adding the modules afterwards to reset all information regarding the previous configurations.

程序保持不变,我看到的唯一区别是添加图标不再是绿色的。还可以尝试在rootFolder/.idea/modules.xml之后删除和添加模块以重置有关先前配置的所有信息。

Gradle based project

基于 Gradle 的项目

Should use "Refresh all Gradle projects" on the Gradle Tool Window (View > Tool Windows > Gradle), represented by the standard refresh icon.

应该在 Gradle 工具窗口(视图 > 工具窗口 > Gradle)上使用“刷新所有 Gradle 项目”,由标准刷新图标表示。

回答by SassyLlama Boi

If you can't recover it via the most upvoted way, you could simply copy another .iml file, paste it into the package* that requires a .iml file, and rename the .iml file to the name of the package. This should work if it is the same type (in my case UTF-8).

如果您无法通过最受好评的方式恢复它,您可以简单地复制另一个 .iml 文件,将其粘贴到需要 .iml 文件的包*中,然后将 .iml 文件重命名为包的名称。如果它是相同的类型(在我的情况下是 UTF-8),这应该可以工作。

*Copy the .iml file by going into package files, right-clicking the package name, and press paste.

*通过进入包文件,右键单击包名称,然后按粘贴来复制 .iml 文件。

You can accomplish this in the left-hand task bar. I have prepared a video demonstration(there's no audio).

您可以在左侧任务栏中完成此操作。我准备了一个视频演示(没有音频)。

回答by g3mini

Create a new file with the project name and paste it. In case you are working with flutter.

使用项目名称创建一个新文件并粘贴它。如果您正在使用颤振。

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
      <excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
      <excludeFolder url="file://$MODULE_DIR$/.idea" />
      <excludeFolder url="file://$MODULE_DIR$/.pub" />
      <excludeFolder url="file://$MODULE_DIR$/build" />
    </content>
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="Dart SDK" level="project" />
    <orderEntry type="library" name="Flutter Plugins" level="project" />
    <orderEntry type="library" name="Dart Packages" level="project" />
  </component>
</module>

回答by Sigurd Knarh?i Johannsen

The Gradle refresh did not work in my case. Several project folders were also missing, so I could not copy/paste new .iml files either.

Gradle 刷新在我的情况下不起作用。几个项目文件夹也丢失了,所以我也无法复制/粘贴新的 .iml 文件。

However when I opened the project anew, choosing Delete Existing Project and Import, the project was able to recreate all the .iml files.

但是,当我重新打开项目时,选择Delete Existing Project and Import,该项目能够重新创建所有 .iml 文件。

回答by DanArl

For Maven projects:

对于 Maven 项目:

  • Open the Maven Tool Window(indicated with this icon: enter image description here)
  • Click the Reimport All Maven Projectsbutton (indicated with this icon: Reimport All Maven Projects).
  • 打开Maven的工具窗口(表示有此图标:在此处输入图片说明
  • 单击重新导入所有 Maven 项目按钮(用此图标表示:重新导入所有 Maven 项目)。

Instructions: https://www.jetbrains.com/help/idea/maven-projects-tool-window.html

说明:https: //www.jetbrains.com/help/idea/maven-projects-tool-window.html

回答by tuiz

My mvn project was not also loading correctly in IntelliJ IDEA after I removed .iml file. I fixed it this way:

删除 .iml 文件后,我的 mvn 项目也没有在 IntelliJ IDEA 中正确加载。我是这样修复的:

  1. Closed IntelliJ IDEA
  2. Deleted .idea dir which was in my project dir.
  3. I reopened IntelliJ IDEA and .idea dir and also .iml was after a while automatically recreated by IntelliJ.
  1. 封闭的 IntelliJ IDEA
  2. 删除了我的项目目录中的 .idea 目录。
  3. 我重新打开了 IntelliJ IDEA 和 .idea 目录,一段时间后,IntelliJ 自动重新创建了 .iml。

Fixed for me.

为我固定。

回答by Chaklader Asfak Arefe

enter image description here

在此处输入图片说明

If you delete the *.imlfile and the .ideafolder and close the IntelliJ, it will be re-generated when you open the project again in the next time.

如果删除*.iml文件和.idea文件夹并关闭IntelliJ,下次再次打开项目时会重新生成。

回答by vitaliis

I also removed .iml file by accident. Here is how it was resolved:

我也无意中删除了 .iml 文件。这是它的解决方法:

  1. Closed IntelliJ IDEA
  2. Reopened program
  3. Selected Import Project -> Create project from existing sources
  4. Clicked Next. It created correct project configuration for me.
  1. 封闭的 IntelliJ IDEA
  2. 重新开放的程序
  3. 选定的导入项目 -> 从现有源创建项目
  4. 单击下一步。它为我创建了正确的项目配置。