IDEA Plugin Dev - Project

About

IDEA - Project from a plugin dev point of view

Structure

_

  • A project consists of one or several modules.
  • Each module includes the plugin source code
  • Each module includes order entries that refer to SDK and libraries the module uses. By default, all modules uses the project SDK.
  • A module can optionally have a set of facets.

Configuration Project File (.ipr)

The project configuration data 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 %project_name%.ipr file.
  • 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.

Project API

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

Note that you don’t need to access project files directly to load or save settings. See Persisting State of Components.

Project

For example, to get the opened project, you can get it from an action:

Project project = e.getProject();

ProjectRootManager: List of source roots for all modules

String projectName = project.getName();
StringBuilder sourceRootsList = new StringBuilder();
VirtualFile[] vFiles = ProjectRootManager.getInstance(project).getContentSourceRoots();
for (VirtualFile file : vFiles) {
  sourceRootsList.append(file.getUrl()).append("\n");
}
Messages.showInfoMessage("Source roots for the " + projectName + " plugin:\n" + sourceRootsList, "Project Properties");

ProjectFileIndex: Is a file belongs to a project / module ?

The ProjectFileIndex interface is used to verify whether a file or directory is related to the specified IDEA project.

  • To get an Instance of the ProjectFileIndex interface: ProjectRootManager.getFileIndex() method.
ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  • A module to which a file belongs? Use the ProjectFileIndex.getModuleForFile(virtualFile) method: This method returns null if the file does not belong to any module.
Module module = projectFileIndex.getModuleForFile(virtualFile);
  • ProjectFileIndex.getContentRootForFile method to get the module content root to which the specified file or directory belongs:
VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFileorDirectory);
  • To get the module source root or library source root to which the specified file or directory belongs. ProjectFileIndex.getSourceRootForFile method. This method returns null if the file or directory does not belong to any source root of modules in the project.
VirtualFile moduleSourceRoot = projectFileIndex.getSourceRootForFile(virtualFileorDirectory);
  • Is a file or directory related to the project libraries. The ProjectFileIndex interface implements 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.

Project SDK

Get

  • To get the project-level SDK: Sdk projectSDK = ProjectRootManager.getInstance(project).getProjectSdk();
  • To get the project-level SDK name: String projectSDKName = ProjectRootManager.getInstance(project).getProjectSdkName();

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

Set

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

Documentation / Reference


Powered by ComboStrap