--- layout: editable title: Plugin Structure --- Plugins are the only supported way to extend IDEA functionality. A plugin uses API exposed by IDEA or other plugins to implement its functionality. This document is focused on the plugin system structure and plugin lifecycle. It doesn't specify any other APIs that may be used by plugins. The following subjects are covered: * [Plugin Content](plugin_content.html) * [Plugin Class Loaders](plugin_class_loaders.html) * [Plugin Components](plugin_components.html) * [Plugin Extensions and Extension Points](plugin_extensions_and_extension_points.html) * [Plugin Actions](plugin_actions.html) * [Plugin Services](TODO) * [Plugin Configuration File](TODO) * [Plugin Dependencies](TODO) # Plugin Services *IntelliJ IDEA* provides the concept of _services_. A _service_ is a plugin component loaded on demand, when your plugin calls the ```getService``` method of the [ServiceManager](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/components/ServiceManager.java) class. *IntelliJ IDEA* ensures that only one instance of a service is loaded even though the service is called several times. A service must have the interface and implementation classes specified in the plugin.xml file. The service implementation class is used for service instantiation. *IntelliJ IDEA* offers three types of services: _application level_ services, _project level_ services and _module level_ services. ## How to Declare a Service? To declare a service, you can use the following extension points in the IDEA core: * *applicationService*: designed to declare an application level service. * *projectService*: designed to declare a project level service. * *moduleService*: designed to declare a module level service. *To declare a service:* 1. Add the appropriate child element (\, \ or \) to the \ section of the plugin.xml file. 2. For the newly added child element, set the following attributes: * *serviceInterface*: specifies the service interface class. * *serviceImplementation*: specifies the service implementation class. Note that the interface and implementation classes can be the same. To clarify the service declaration procedure, consider the following fragment of the plugin.xml file: ```xml ``` ### How It Works? To instantiate your service, in Java code, use the following syntax: ```java MyServiceImplClass service = ServiceManager.getService(MyServiceImplClass.class); ``` ### Sample Plugin This section allows you to download and install a sample plugin illustrating how to create and use a plugin service. This plugin has a project component implementing a service that counts a number of currently opened projects in *IntelliJ IDEA*. If this number exceeds the maximum allowed number of simultaneously opened projects, the plugin returns an error message and closes the most recently opened project. *To install and run the sample plugin* * Click [here](/attachments/MaxOpenedProjects.zip) to download the .Zip archive that contains the sample plugin project. * Extract all files from the .Zip archive to a separate folder. * Start *IntelliJ IDEA*, on the starting page, click *Open Project*, and then use the *Open Project* dialog box to open the downloaded project *MaxOpenedProjects*. * On the main menu, choose *Run \| Run* or press *Shift + F10*. * If necessary, change the [Run/Debug Configurations](http://www.jetbrains.com/idea/webhelp/run-debug-configuration-plugin.html). # Plugin Configuration File plugin.xml The following is a sample plugin configuration file. This sample showcases and describes all elements that can be used in the plugin.xml file. ```xml VssIntegration VssIntegration Vss integration plugin Initial release of the plugin. 1.0 Foo Inc. MyFirstPlugin MySecondPlugin messages.MyPluginBundle com.foo.Component1Interface com.foo.impl.Component1Impl com.foo.Component2 com.foo.Component3 ``` # Plugin Dependencies In your plugin, you may depend on classes from other plugins, either bundled, third-party or your own. In order to do so, you need to perform the following two steps: * Add the jars of the plugin you're depending on to the classpath of your IntelliJ IDEA SDK. (**Note**: Don't add the plugin jars as a library: this will fail at runtime because IntelliJ IDEA will load two separate copies of the dependency plugin classes.) * Add a tag to your plugin.xml, adding the ID of the plugin you're depending on as the contents of the tag. For example: ```xml org.jetbrains.idea.maven ``` To find out the ID of the plugin you're depending on, locate the META-INF/plugin.xml file inside its jar and check the contents of the tag.