Structure of IntelliJ IDEA Project

Skip to end of metadata
Go to start of metadata

This document is a work in progress.

This topic considers the concept of IntelliJ IDEA projects and related subjects, such as modules, facets, libraries, SDK.
The project structure and Java classes you can use to manage projects and modules have been considered.

Project and Its Components

This section briefly discusses the IDEA project structure, project components and related terms. For more information about projects and their components, refer to Project , Module , Library , and Facet in IntelliJ IDEA Web Help.

Project

In IntelliJ IDEA, a project encapsulates all your source code, libraries, build instructions into a single organizational unit. Everything you do in IntelliJ IDEA, is done within the context of a project. A project defines some collections referred to as modules and libraries. Depending on the logical and functional requirements to the project, you can create a single-module or a multi-module project.

Module

A module is a discrete unit of functionality that can be run, tested, and debugged independently. Modules includes such things as source code, build scripts, unit tests, deployment descriptors, etc. In the project, each module can use a specific SDK or inherit SDK defined on the project level (see the SDK section later in this document). A module can depend on other modules of the project.

Library

A library is an archive of compiled code (such as JAR files) that your modules depend on. IntelliJ IDEA Community Edition supports three types of libraries:

  • Module Library: the library classes are visible only in this module and the library information is recorded in the module *.iml file.
  • Project Library: the library classes are visible within the project and the library information is recorded in the project *.ipr file.
  • Global Library: the library information is recorded in the applicationLibraries.xml file into the <User Home>/.IntelliJIdea/config/options directory. Global libraries are similar to project libraries, but are visible for the different projects.

For more information about libraries, refer to Library .

SDK

Every project uses a Software Development Kit (SDK). For Java projects, SDK is referred to as JDK (Java Development Kit). SDK determines which API library is used to build the project. If your project is multi-module, the project SDK by default is common for all modules within the project. Optionally, you can configure individual SDK for each project module.
For more information about SDKs, see JDKs and Configuring Project JDK in IntelliJ IDEA Web Help.

Facet

A facet represents certain functionality, associated with a module, that tells IntelliJ IDEA how to treat the module contents. A module can have multiple facets.
In IntelliJ IDEA Ultimate Edition, you can add or change facets of existing modules using IntelliJ IDEA UI (see Facet and Facet Dependencies in IntelliJ IDEA Web Help).
In IntelliJ IDEA Community Edition, you can manage facets using API. For more information and samples, refer to Working with modules later in this document.

Project Structure

From the plugin developper's point of view, an IDEA project can be though of as follows:

A project consists of one or several modules. Each module includes the plugin source code and so called order entries that refer to SDK and libraries the module uses. By default, all modules uses the project SDK. In adition, a module can optionally have a set of facets.
This document explains how you can explore and change the structure of the IDEA plugin projects using API.

Working with Projects

This section explains how to complete some common tasks related to management of the IDEA plugin projects. The Java classes and interfaces that you can use to explore and change the project contents are discussed.

How to Work with Project Files?

IntelliJ IDEA stores the project configuration data in XML files. The list of those files depends on the plugin project format .

For file-based format projects, the information core to the project itself (e.g. location of the component modules, compiler settings, etc.) is stored in the <%plugin name%>.IPR file. The information about modules the project includes is stored in <%module name%>.IML files. Module files are created for each module.

For directory-based format projects, the project and workspace settings are stored in a number of XML files under the <%Plugin home directory%>/.idea directory. Each XML file is responsible for its own set of settings and can be recognized by its name: projectCodeStyle.xml, encodings.xml, vcs.xml etc. As for the file-based format projects, .IML files describe modules.

To work with projects and project files, you can use the following classes and interfaces:

How do I get plugin project file?

For file-based format projects:

  • To get the full path to the project .IPR file: String pathToFile = project.getProjectFile().getUrl();
  • To get the .IPR file name: String fileName = project.getProjectFile().getName();

For directory-based format projects, the same instructions allow you to get the path to the misc.xml file stored into the <%Plugin home directory%>/.idea directory.

Note that hereafter, the project variable is of the Project type. For example, for the opened plugin, you can get it from an action: Project project = e.getData(PlatformDataKeys.PROJECT);

How do I get plugin home directory?

Use String pluginHomeDirectory = project.getBaseDir().getUrl();
This does not depend on the plugin project format.

How do I get a list of source roots for all modules in my project?

Use the ProjectRootManager.getContentSourceRoots() method. To clarify this, consider the following code snippet:

How do I check whether a file is related to a project?

IntelliJ IDEA provides the ProjectFileIndex interface you can use to verify whether a file or directory is related to the specified IDEA project.
This section explains how you can use this interface.

How do I get an instance of the ProjectFileIndex interface?

Use the ProjectRootManager.getFileIndex() method. For example:
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();

How do I get a module to which a file belongs?

To determine a module in the project in question to which the specified virtual file belongs, use the ProjectFileIndex.getModuleForFile(virtualFile) method:
Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile);
Note that this method returns null if the file does not belong to any module.

You can also use the ProjectFileIndex.getContentRootForFile method to get the module content root to which the specified file or directory belongs:

VirtualFile moduleContentRoot = ProjectRootManager.getInstance(project).getFileIndex().getContentRootForFile(virtualFileorDirectory);

How do I get the module source root or library source root to which the specified file or directory belongs?

Use the ProjectFileIndex.getSourceRootForFile method. For example:
VirtualFile moduleSourceRoot = ProjectRootManager.getInstance(project).getFileIndex().getSourceRootForFile(virtualFileorDirectory);
Note that this method returns null if the file or directory does not belong to any source root of modules in the project.

How do I check whether a file or directory is related to the project libraries?

The ProjectFileIndex interface imlements a number of methods you can use to check whether the specified file belongs to the project library classes or library sources.
You can use the following methods:

  • ProjectFileIndex.isLibraryClassFile(virtualFile): Returns true if the specified virtualFile is a compiled class file.
  • ProjectFileIndex.isInLibraryClasses(virtualFileorDirectory): Returns true if the specified virtualFileorDirectory belongs to library classes.
  • ProjectFileIndex.isInLibrarySource(virtualFileorDirectory): Returns true if the specified virtualFileorDirectory belongs to library sources.

How do I get the project SDK?

  • To get the project-level SDK: Sdk projectJDK = ProjectRootManager.getInstance(project).getProjectJdk();
  • To get the project-level SDK name: String projectJDKName = ProjectRootManager.getInstance(project).getProjectJdkName();

How do I set the project SDK?

  • To set the project-level SDK: ProjectRootManager.getInstance(project).setProjectJdk(Sdk jdk);
  • To set the project-level SDK name: ProjectRootManager.getInstance(project).setProjectJdkName(String name);

Note that by default, the project modules use the project SDK. Optionally, you can configure individual SDK for each module.

Working with Modules

IntelliJ IDEA provides a number of Java classes and interfaces you can use to work with modules:

This section discusses how to complete some common tasks related to management of modules.

How do I get a list of modules the project includes?

Use the ModuleManager.getModules() method.

How do I get the module file?

To get the path to the module file .IML, use the Module.getModuleFilePath() method.
To get the module file .IML, use the Module.getModuleFile() method.

To clarify this, consider the following code snippet:

This outputs the list of all module files and modules the specified project includes.

How do I get order entries for a module?

Order entries include SDK and libraries the module uses. With the IntelliJ IDEA UI, you can view order entries for a module on the Dependencies tab of the Project Structure dialog box.
To explore the module dependencies , use the OrderEnumerator class and the OrderEntry interface.

To get a list of order enries, use the getOrderEntries() method of the ModuleRootManager class.
The following code snippet illustrates how you can get a list of order entries for each module the project includes:

How do I get the SDK the module uses?

Use the ModuleRootManager.getSdk() method. This method returns a value of the Sdk type.
The following code snippet illustrates how you can get detailed information on SDK the specified module uses:

How do I get a list of modules on which this module directly depends?

Use the ModuleRootManager.getDependencies() method to get an array of the Module type values or the ModuleRootManager.getDependencyModuleNames() to get an array of module names. To clarify, consider the following code snippet:

How do I get a list of modules that depend on this module?

Use the ModuleManager.getModuleDependentModules(module) method.

Note that you can also check whether a module (module1) depends on another specified module (module2) using the ModuleManager.isModuleDependent method in the following way:
boolean isDependent = ModuleManager.getInstance(project).isModuleDependent(module1,module2);

How do I get a module to which the specified file or PSI element belongs?

  • To get the project module to which the specified file belongs, use the ModelUtil.findModuleForFile() static method. To clarify, consider the following code snippet:
  • To get the project module to which the specified PSI element belongs, use the ModelUtil.findModuleForPsiElement(psiElement) method.

How do I work with libraries available within a module?

How do I get a list of libraries available within a module?

To get the list of libraries, you can enumerate all order entries for the module of interest (see "How do I get order entries for a module?" earlier in this document), and then select the order entries that are instances of the LibraryOrderEntry interface. To clarify this, consider the following code snippet that illustrates how to output the list of libraries for the specified module:

This sample code outputs a list of libraries for the module module. In the list, each entry includes the library name and level (such as module, project or application level).

How do I get the library content?

The Library.ModifiableModel class provides the getUrls method you can use to get a list of source roots and classes the library includes. To clarify, consider the following code snippet:

In this sample code, lib is of the Library type.

How do I get a set of facets the module includes?

Use the DefaultFacetsProvider and Facet classes.
The following code snippet illustrates how to get a list of names of facets for each module the specified plugin project includes:

to be continued

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.