IntelliJ IDE | .iml File lost or deleted

丶灬走出姿态 提交于 2019-12-08 14:26:21

问题


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

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

Error Loading Project

Cannot load module FirstWebApp

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

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.

  • How can such a file just disapear?

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

  • What do these iml files do?

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


回答1:


*.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.

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:

Gradle based project

Click the Sync Project with Gradle Files button (this one ) in the toolbar;

Maven based project

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

Edit:

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

Using Intellij Community 2018.1 for this update:

Maven based project

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

Gradle based project

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




回答2:


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

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

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




回答3:


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.

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




回答4:


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

  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.

Fixed for me.




回答5:


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>



回答6:


For Maven projects:

  • Open the Maven Tool Window (indicated with this icon: )
  • Click the Reimport All Maven Projects button (indicated with this icon: ).

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



来源:https://stackoverflow.com/questions/36542288/intellij-ide-iml-file-lost-or-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!