Intellij Idea 简明教程
Intellij Idea - Deep Dive
本教程的前四章旨在让初学者对 IntelliJ 有一个基础了解。本部分深入探讨 IntelliJ,并讨论更多有关项目、其格式以及其他内容。
The first four chapters of this tutorial were designed to give beginners a basic-level overview of IntelliJ. This section dives deep into IntelliJ and discusses more about projects, its format along with other things.
Understanding Projects
项目是指你正在开发的应用程序或软件。它可以包含多个模块、类、库、配置等等。它处于层次结构中的最顶端。
A project is an application or software on which you are working. It can contain multiple modules, classes, libraries, configuration, and so on. It is the topmost element in the hierarchy.
Understanding Modules
模块位于“项目”下列的一级。模块是一个独立的实体,可以独立于其他模块进行编译、调试和运行。单个项目可以包含多个模块。你可以随时添加或删除项目中的模块。
Modules feature one rung below ‘Project’. A module is a separate entity that can be compiled, debugged and run independently of other modules. A single project can contain multiple modules. You may add or remove modules from a project any time.
除此之外,我们还可以导入现有模块。请按照下列步骤导入现有模块 −
In addition to this, we can also import the existing modules. Follow these steps to import the existing modules −
-
Go to File → Project structure.
-
Select modules and click on plus icon.
-
It will show the option to import module.
Understanding Folders
Content root 是一个包含构成模块的所有文件的文件夹。一个模块可以有多个内容文件夹。文件夹分以下类型 −
Content root is a folder that contains all the files that make up your module. A module can have more than one content folder. Folders are categorized into the following types −
-
Sources − By assigning this category to folder, we instruct IntelliJ that this and its subfolder contain java source code and it should be compiled as part of the compilation process.
-
Tests − By assigning this category to folder, we instruct IntelliJ that it is the location for unit tests. This folder can access classes from the Sources folder.
-
Resources − It represents various resource files used in project, for instance, images, XML and properties files. During the compilation process, contents of this folder is copied as is to the output folder.
-
Excluded − Contents from the excluded folder will not be indexed by IntelliJ. This means that IntelliJ will not provide the code completion suggestion and other hints. For example, the output directory and the target directory are excluded by default.
-
Test resources − This is similar to the resources and used for unit tests.
Understanding Libraries
库是由不同类的编译组成的。库使代码可以重用。在 Java 中,库可以封装在 ZIP、Jar 或简单的文件夹中。我们可以分三个不同级别定义库。级别为 - 全局、项目和模块级别。
Library is a compilation of different classes. Library enables code reusability. In Java, library can be enclosed in ZIP, Jar or simply folder. We can define libraries at three different levels. The levels are - global, project and module level.
-
Global level − Shared by all projects.
-
Project level − Shared by all the modules of project.
-
Module level − Shared by the classes of those modules.
Understanding Facets
构面是模块的扩展。它们增加对框架和技术的支持。将构面添加到模块时,IntelliJ 会识别出增加的支持。举例而言,编辑器中的提示和帮助、窗口栏中的新工具、依赖项下载等等。您可以从 File → Project Structure 窗口添加构面,如下所示:
Facets are the extensions to the modules. They add support to the frameworks and technologies. When a facet is added to a module, IntelliJ identifies it adds support. For instance, hints and help in editor, new tools in window bar, dependency downloading and so on. You can add facets from the File → Project Structure window as shown below −
Artifacts
构建产品是项目的输出。可以是简单的 JAR 文件、Java EE 应用程序或 Java EJB 应用程序。如果我们使用 Gradle 或 Maven 等外部构建工具,则 IntelliJ 会自动为它们添加构建产品。可以导航至 File → Project Structure 创建构建产品,如下所示:
Artifacts are the output of a project. It can be a simple JAR file, Java EE application, or Java EJB application. If we are using external build tools like Gradle or Maven, then IntelliJ will add artifact for them automatically. Artifacts can be created by navigating to the File → Project Structure as shown below −
Importing Existing Project
在本节中,我们将了解如何导入现有项目。我们可以通过两种方式导入项目:
In this section, we will understand how to import an existing project. We can import a project in two ways −
-
Import it from existing source
-
Import it from the build model.
当前,它支持 Gradle 和 Maven 构建工具。要导入项目,请:
At present, it supports Gradle and Maven build tools. To import project −
-
Navigating to File → New → Project from the existing source.
-
Select directory of the existing project, Maven’s pom.xml or Gradle’s build script.
-
Click on the OK button.
Project Formats
IntelliJ 支持两种项目格式,一种是 directory-based ,另一种是 file-based 。基于目录的格式较新,建议使用。默认情况下,IntelliJ 会创建基于目录的项目格式。您可以在创建新项目时选择项目格式。在新建项目窗口中,只需单击更多设置,如下面的图片所示:
IntelliJ supports two types of project format one is directory-based and other is file-based. Directory based format is newer one it is recommended. By default, IntelliJ creates directory based project format. You can select project format while creating new project. On new project window just click on more settings as show in below image −
Directory-based Project Format
此格式有助于在项目中创建一个 idea 文件夹,并将项目中的所有配置文件保留在此文件夹中。设置被分组到 xml 文件中。举例而言,它将创建 misc.xml, modules.xml, workspace.xml 等。以下屏幕截图将帮助您了解它的工作原理:
This format helps create an idea folder in your project and keep all configuration files inside that folder. Settings are grouped into the xml files. For instance, it will create misc.xml, modules.xml, workspace.xml and so on. The following screenshot will help you understand how this works −
File-based Project Format
它将创建两个项目文件,分别带有 ..ipr 和 wpr 扩展名。ipr 文件将包含特定于项目的设置,而 wpr 文件将包含特定于工作区设置。
It will create two project files with ..ipr and wpr extensions. The ipr file will contain project-specific settings and the wpr file will contain workspace-specific settings.
要将基于文件的项目转换为基于目录的项目,请转至 File → Save as a Directory-Based format 。
To convert a file-based project to a directory-based project, go to the File → Save as a Directory-Based format.
Directory-based vs File-based Project Format
与基于文件的项目格式相比,基于目录的项目格式将设置存储在带有有意义名称的单独文件夹中。其它差异:
Compared to the file-based project format, the directory-based project format stores settings in a separate folder with meaningful names. Other differences are −
-
Related settings getting stored in a single file makes it easier to manage in directory-based project format.
-
If folder contains the idea subfolder then IntelliJ recognizes that project. Because of this, you don’t have select the ipr project explicitly.
-
Directory-based project format breaks settings into multiple files hence it is easier to select particular type of setting to store in version control system.