IntelliJ IDEA中各种文件夹标记的区别

本文详细解析了IntelliJ IDEA中不同类型的文件夹,包括Source roots、Test source roots、Resource roots、Test resource roots及Excluded roots的功能与用途。Source roots用于存放待编译的源代码;Test source roots专为测试代码设计;Resource roots存储应用资源文件;Test resource roots关联测试源码的资源;Excluded roots则被IDEA几乎忽略。

在IntelliJ IDEA中可以将文件夹标记为各种root文件夹,大致如下图所示:

这些root文件夹有何区别呢?

1. Source roots (or source folders)

By assigning a folder to this category, you tell IntelliJ IDEA that this folder and its subfolders contain source code that should be compiled as part of the build process.

通过将文件夹加入这种类别,来告诉IntelliJ IDEA,这个文件夹和它的子文件夹中包含源码,在构建工程时,需要作为一部分被编译进去。

2. Test source roots (or test source folders; shown as rootTest)

These roots are similar to source roots but are for code intended for testing (e.g. for unit tests). Test source folders let you keep the code related to testing separate from the production code.

Compilation results for sources and test sources, normally, are placed into different folders.

这个类型的文件夹也用于存放源码,不过是测试的源码(比如单元测试)。test source 文件夹可以帮助你将测试代码和产品代码分离开。

3. Resource roots

are for resource files used in your application (images, various configuration XML and properties files, etc.).

During the build process, all the contents of the resource folders are copied to the output folder as is.

Similarly to sources, you can specify that your resources are generated. You can also specify which folder within the output folder your resources should be copied to.

该类文件夹用于存放你的应用中需要用到的资源文件(如:图片、xml或者properties配置文件等)。

在构建过程中,resources文件夹中的内容均会按照原文件的样子被复制到输出文件夹。和sources文件夹一样,你可以定制你的resources文件夹的结构。你也可以指定你的resources文件夹中的文件被拷贝到输出文件的哪个文件夹中。

ps:默认情况下,工程编译后,resources中的文件和文件夹会被放置在和源码编译后相同的文件夹中,所以如果在源码中以相对路径读取resources中的配置文件时,可认为src中的java文件夹和resources为同一个根目录。

4. Test resource roots

(or test resource folders; shown as rootTestResourceIJ; available only in Java modules) are for resource files associated with your test sources. In all other respects, these folders are similar to resource folders.

(只有在java模式中可以使用)用于存放测试源码中关联的资源文件。除此之外,和resource文件夹没有区别。

**5. Excluded roots (shown as rootExcluded) are ones that IntelliJ IDEA 
“almost ignores”.**

Very limited coding assistance is provided for files in excluded folders. Classes contained in excluded folders don’t appear in code completion suggestion lists, references to such classes are shown in the editor as unresolved. When searching, IntelliJ IDEA doesn’t look in excluded folders, etc.


另外补充一下IntelliJ IDEA中的各种文件夹图标:

### 如何在 IntelliJ IDEA 中显示隐藏的 `.git` 文件夹IntelliJ IDEA 中,默认情况下某些文件和文件夹会被视为隐藏资源而不予显示,例如 `.git` 文件夹。为了使这些隐藏的文件或文件夹可见,可以通过调整项目的设置来实现。 #### 方法一:通过项目视图设置显示隐藏文件 可以在 **Project View Settings** 中启用显示隐藏文件的功能: 1. 打开 IntelliJ IDEA 并进入目标项目。 2. 在左侧的 **Project 工具窗口** 中右击任意位置,选择 **Show Options Menu** 或点击齿轮图标。 3. 勾选选项 **"Show Excluded Files"** 和 **"Show Hidden Files"**[^1]。 此操作允许开发者查看被排除在外的文件以及操作系统标记为隐藏的文件(如 `.git` 文件夹)。需要注意的是,这仅影响当前项目的可视化效果,并不会改变实际磁盘上的文件状态。 #### 方法二:修改全局设置以始终显示隐藏项 如果希望所有新建或打开的项目都能自动展示隐藏文件,则可以更改全局偏好设定: 1. 转至菜单栏路径 `File -> Settings...` (对于 macOS 用户则是 `IntelliJ IDEA -> Preferences`)。 2. 寻找并导航到节点 `Appearance & Behavior -> System Settings -> File Watchers` 下方的相关部分[^2]。 3. 尝试寻找是否有针对特定类型的忽略规则存在;如果有涉及 Git 的过滤器,请取消勾选它或将之移除。 4. 另外确认是否存在其他可能屏蔽`.git`目录显示的因素——比如自定义scope定义里包含了exclude pattern之类的内容也需要相应调整过来使其不匹配.git路径即可恢复正常浏览体验。 另外值得注意的一点是当遇到像引用提到过的那种由于环境变量配置不当所引发的具体错误情况时(即引用[3]),则需额外注意检查自己的系统级与用户级别的PATH环境变量是否正确设置了Git命令行工具的位置,同时也要保证IDE本身的临时工作区未受到破坏从而导致类似“无法定位程序输入点”的异常状况发生. 最后附上一段简单的Python脚本用于演示如何遍历指定路径下的所有子目录及其内部成员,其中包括那些通常被认为是非标准可视范围内的条目: ```python import os def list_all_files(directory): for root, dirs, files in os.walk(directory): print(f"In directory {root}:") if '.git' not in dirs and '.git' in os.listdir(root): # 特殊处理 .git 文件夹 dirs.append('.git') for d in dirs: print(os.path.join(root,d)) for f in files: print(os.path.join(root,f)) if __name__ == "__main__": target_dir = r"C:\path\to\your\project" list_all_files(target_dir) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值