2015-05-07 13:55:41 +02:00

5.0 KiB

layout title
editable Project Model. Roots and Libraries. Configuring Project from Code.

This section considers internal architecture IntelliJ Platform projects and gives overview for classes and packages of the API used to manipulate with projects and their components, such as modules, facets, libraries, SDK. For general information about the concept of a projects and concepts related to it, refer to Project, Module, Library, and Facet in IntelliJ IDEA Web Help.

Working with Projects

IntelliJ Platform stores the project configuration data in XML files. The list of those files depends on the plugin [project] (http://www.jetbrains.com/idea/webhelp/project.html) 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 <%project 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 <%Project 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.

Main classes providing work with the project model are located in the package projectModel-api.openapi. Basic API classes and interfaces for the concepts of [project] (https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/project/Project.java), [module] (https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/module/Module.java), [application] (https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/application/Application.java), and [component] (https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/components/ProjectComponent.java) are placed in the [core-api.openapi] (https://github.com/JetBrains/intellij-community/tree/master/platform/core-api/src/com/intellij/openapi) package.

Finding Source Roots

To get an array of all the source roots for a project use ProjectRootManager.getContentSourceRoots() method like this code snippet shows:

VirtualFile[] vFiles = ProjectRootManager.getInstance(project).getContentSourceRoots();

Checking if File Belongs to a Project

Use [ProjectFileIndex.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/projectModel-api/src/com/intellij/openapi/roots/ProjectFileIndex.java) to get this information.

Getting an Instance of the ProjectFileIndex Interface

Use the ProjectRootManager.getFileIndex() method. For example:

ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();

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

[Code sample] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/project_model/src/com/intellij/plugins/project/model/ProjectFileIndexSampleAction.java)

Changing the project structure

Utility classes which can be used for modifying a project structure can be found in the package [projectModel-impl.openapi] (https://github.com/JetBrains/intellij-community/blob/master/platform/projectModel-impl/src/com/intellij/openapi). It's [roots] (https://github.com/JetBrains/intellij-community/blob/master/platform/projectModel-impl/src/com/intellij/openapi/roots/) subpackage contains instances and utilities meant to work with project and module source roots, including [ModuleRootModificationUtil.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/projectModel-impl/src/com/intellij/openapi/roots/ModuleRootModificationUtil.java) and [ProjectRootUtil.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/projectModel-impl/src/com/intellij/openapi/projectRoots/impl/ProjectRootUtil.java)

Refer to the [basic example] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/project_model/src/com/intellij/plugins/project/model/ModificationAction.java) of on-the-fly project structure modification to learn how it can be implemented.