IntelliJ IDEA Essentials
上QQ阅读APP看书,第一时间看更新

What is a project?

Almost everything you do in the IDE is contained within the scope of a project. The project represents the software you are working on—it is the top-level container for all your modules, libraries, and configuration settings. There can be only one project open in a single IDE window. If you would like to have multiple projects open, IntelliJ IDEA will open them in separate, isolated windows. You can switch between the windows using the Next Project Window or Previous Project Window options from the Window menu or using the keyboard shortcut, Ctrl + Alt + open/close bracket.

IntelliJ IDEA stores the project's configuration in two different formats. The project's format is covered later in the Project format section.

Project structure and configuration

Before we create a new project from scratch or import an existing one, we will focus on IntelliJ IDEA's key concepts that will define the project. We will start by listing the terminology differences between different IDEs and then explain all of the concepts.

Comparison of Eclipse, NetBeans, and IntelliJ IDEA terminologies

IntelliJ IDEA has a different terminology when compared to the terminologies used in Eclipse and NetBeans. The following table compares the Eclipse and IntelliJ IDEA terminologies:

The next table will highlight the differences in the terminology used in NetBeans and IntelliJ IDEA:

Now that we have seen the differences, let's have a closer look at these concepts.

The project

The project is the concept that represents a complete software application. It doesn't contain the source code and other development files such as the build or configuration scripts. It defines the project-wide settings and contains the collection of modules and libraries.

Modules

A project in IntelliJ IDEA consists of modules. A module is a discrete, separate entity that can be compiled, run, and debug autonomously from other modules in the project. The module can contain source code, resources, scripts, documentation, or other files.

At any time, you can import, add, or remove the module from the project using the Project Structure dialog box, by navigating to File | Project structure. To create or import a new module, use the + button on the toolbar or use the Alt + Insert keyboard shortcut:

Of course, you can add a new module to an existing project. Modules can also depend on each other—they can be added as dependencies for other modules. To create such a dependency, click on Module Dependency… and use the Alt + Insert keyboard shortcut, as shown in the following screenshot:

In the next dialog box, select one or more modules that the current modules should depend on, as shown in the following screenshot:

IntelliJ IDEA stores module configurations in a file with the .iml extension. You can put the .iml files into version control to be able to share module specific configuration easily.

It might be worth mentioning that if you are using external build tool such as Maven or Gradle in your project, these files will be regenerated by opening or importing the project. In this case, it's better not to store IntelliJ IDEA module configuration files in the version control system.

Folders

The structure of the module is represented by the folders on the disk. It begins with Content Root. You can have multiple content roots in the module. Each content root can have different types of folders:

  • Sources: These folders contain your source files. They will be analyzed and indexed, and they are available to the compiler. If you develop in Java, the structure of the subfolders will match the package hierarchy.
  • Tests: This is the location of your unit tests. The files from these folders will be compiled and available to run tests. The compile output will go to the same output folder as for the Sources folders. The source code in the Sources folder is visible to the code in the Tests folder, but not vice versa. We will discuss running unit tests in Chapter 7, Red or Green? Test Your Code.
  • Excluded: The contents of the Excluded folders will not be indexed by IntelliJ IDEA. It means that you will not get parameter completion hints for source files contained in the folder of that type. IDE will know nothing about files in such folder and will ignore it. The compiler output directory, target, is excluded by default. The excluded directories and their contents are also not displayed in the tree structure in the Project tool window. It is usually good to exclude folder with large files which you don't want to be indexed, for example, logfiles.

    Tip

    Indexing large files can slow down your IDE, so think about excluding them.

  • Resources: Files in the resource folders will be simply copied untouched to the output directory during the compiling process. Store your configuration, properties, and static resources here. For a project imported from Maven, the Maven compiler will do resource filtering based on the configuration in pom.xml and the contents of these folders will be changed.
  • Test Resources: This is similar to the Resources folders, but it's used for unit tests.

To set up the content root and folder types, open the Project Structure dialog box, select the desired module, and switch to the first tab, Sources. In the following screenshot, you can see that we add or pick the content root and then mark a folder with the appropriate folder type:

You can also mark a folder with the specified type by using the context menu, which can be accessed using the right-mouse button or using the respective keyboard shortcut, as shown here:

Libraries

A library is set of compiled code or resources that the module uses during compilation or runtime. If you develop in Java, this will be the set of class files enclosed in the JAR and ZIP files or directories. On the other hand, if you develop in other languages such as JavaScript, for instance, the library will consist of one or more JavaScript files similar to Angular.js or React.js.

The libraries are defined on the following three levels:

  • Global (IDE) level: The global libraries will be shared by all projects. Use this level to set up the common libraries that you use across all of your projects, such as Apache Commons or Google Guava.
  • Project level: The project libraries are common for all of the modules in the project. Use this to set up the library that will be used by more than one module.
  • Module level: The library that will be recognized only by the module in which it is defined. Use this level to set up the library, which is only appropriate to a specific module.

    Tip

    When you set up the library, include the library source and documentation. IntelliJ IDEA will use this to provide you with hints such as parameters completion and API documentation during the coding process.

As with modules, you can add or remove the library from the project at any time using the Project Structure dialog box, by navigating to File | Project structure.

To add the project level library, click on Libraries and then click on the green plus icon. Alternatively, use the keyboard shortcut Alt + Insert as shown here:

Creating the library using the From Maven... option is a brilliant feature that makes the creation of simple projects much easier. Just enter the name of the library you are looking for in the dialog box and use the keyboard shortcut Shift + Enter, as shown in the following screenshot:

You don't need to search for and download the library manually from the Internet any more. Pick the required library from the drop-down list and IntelliJ IDEA will do it for you automatically.

To add a library on the module level, select the module, switch to the Dependencies tab, and then click the green plus icon or use the keyboard shortcut Alt + Insert as shown here:

Actually, when it comes to Java development, managing libraries manually can be a painful process. I highly recommend using a dependency manager such as Maven, Gradle, or Ivy to set up the libraries. Maven build files are first-class citizens in IntelliJ IDEA. If you import the Maven project, the IDE will parse the build file and manage the libraries list automatically. We will talk about importing Maven projects later in this chapter.

Facets

Think about facets as an extension or as the "nature" of the module. Facets add support for frameworks and technologies to your module. When you add the facet to the module, IntelliJ IDEA will recognize the particular technology, language, or framework and then give you proper support. This support will consist of hints and helpers in the editor, availability of new tool windows, or the possibility of downloading framework libraries. For instance, the Spring facet allows you to navigate between Spring XML configuration files with Ctrl + mouse click, displays the autowiring icons, and lets you navigate to the autowired dependencies in your Java code. If you don't enable the Spring facet, you don't get any of that awesome functionality.

Some of the facets available in the IDE are Android, Web, Struts, Spring, Hibernate, and so on.

Facets can be added to the module manually using the Project Structure dialog box, or they can be detected by IntelliJ IDEA. To add the facet manually, switch to the Facets section and click on the green plus icon or use the keyboard shortcut Alt + Insert as shown here:

IntelliJ IDEA scans the module source code, and if it finds the file characteristic of a certain framework, it suggest the matching facet. Just click on the Configure link to create facets.

IntelliJ IDEA will then display the facet configuration dialog box, for example, the Setup Frameworks dialog box where you can set up the new facet, as shown in the following screenshot:

Note that a single module can have multiple facets. Expand the module branch to see the facets contained in the module, as shown in the following screenshot:

The module with two facets

Artifacts

Artifacts refer to the output of the project. Think about the artifact as a recipe that can be used to create and package the output. This can be a simple JAR resulting from compiling the Java module or a very complex, deployable file such as Java EE Enterprise ARchive (EAR).

If you're using Maven, Gradle, or a similar build tool, you don't need to bother about manually creating artifacts—IntelliJ IDEA will create the desired artifacts automatically. There is also another advantage of using the external build tool: you will be able to build and package your project outside of the IDE. This can be useful when deploying the application or running the build in the continuous integration system. We will focus on the external build process in Chapter 6, Building Your Project. Anyhow, when required, you can create artifacts manually in the IDE. To add and remove artifacts, use the Project Structure dialog. You can either choose the predefined artifact type, for example, Web Application: Archive to create the .war file, or select Other to create custom one, as shown in the following screenshot:

To set up the contents of artifact, pick the item (let it be library, module, or another artifact from the Available Elements list) and drag your artifact contents to the left.

Any item has the context menu available when you right-click on it, we can present the default predefined locations in the artifact for that item. This is shown in the following screenshot:

Artifact element's context menu

Tip

You can double-click on the element to put it in the default predefined place within the artifact.

To see precisely what will be included in the output, select the Show content of elements checkbox. You will see which files and directories will actually go to the generated output as shown here:

Displaying the artifact contents

Once defined, artifacts are available in the Build artifacts command in the Build menu. We will more about building the module in Chapter 6, Building Your Project.

Tip

There is actually no limit for the number of artifacts in the project; create as many as you like!