Plugin Initialization on Startup  General

Plugin Initialization on Startup

This post explains how code can be executed at application startup or as soon as a new project is opened. It also outlines other possibilities how to run code to initialize your plugin.

Sample code @ github: Different ways to run code on startup.

Application startup

It happens quite often that some code must be run as soon as the plugin is loaded. There are several ways to achive this.

If you need init & dispose methods, then implement an application component. If you don’t need dispose functionality, then a preloading activity could be implemented. If you need to access the current project in that code, please look below for further explanations.

Application components

An application component provides hooks to run code on application startup and shutdown. Make sure that you don’t block the IDE by performing time-consuming actions.

Application components of your plugin are always loaded, even if no other component is actually using it. If init-on-demand is possible you should implement an application service.

If you would like to run long-running actions but don’t need to dispose on shutdown you could do this by implementing a preloadingActivity extension instead.

1
2
3
4
5
6
<!-- in plugin.xml -->
<application-components>
    <component>
      <implementation-class>com.ansorgit.intellijDocs.startupActivity.MyApplicationComponent</implementation-class>
    </component>
  </application-components>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//Java class
public class MyApplicationComponent implements ApplicationComponent {
    private static final Logger LOG = Logger.getInstance(MyApplicationComponent.class);

    public void initComponent() {
        LOG.info("Initializing plugin data structures");
    }

    public void disposeComponent() {
        LOG.info("Disposing plugin data structures");
    }

    @NotNull
    public String getComponentName() {
        return "myApplicationComponent";
    }
}
Application component declaration in plugin.xml
MyApplicationComponent.java
Sample code on github
ApplicationComponent.java
The interface definition as provided by JetBrains

Preloading activity extension points

If you would like to run some time-consuming or possibly blocking background activity on startup you could implement the preloadingActivity extension point. Implementations of PreloadingActivity are executed in a separate thread in IntelliJ, one at a time. A ProgressIndicator is available to make sure that the application has not been shutdown.

1
2
<preloadingActivity
    implementation="com.ansorgit.intellijDocs.startupActivity.MyPreloadingActivity"/>
1
2
3
4
5
6
7
public class MyPreloadingActivity extends PreloadingActivity {
    private static final Logger LOG = Logger.getInstance(MyPreloadingActivity.class);

    public void preload(@NotNull ProgressIndicator indicator) {
        LOG.info("Preloading plugin-data");
    }
}
Preload action declaration in plugin.xml
MyPreloadingActivity.java
Sample code on github
PreloadingActivity.java
The interface definition of the preloadingActivity extension.
Preloader.java
IntelliJ’s class which calls all available implementations of preloadingActivity.

Application shutdown

To execute code on application shutdown implement the disposeComponent method of an application component.

Project initialization

The application is only initialized once, but there may be multiple projects open at the same time. It’s also possible that no project is opened, e.g. on first startup. If you need access to the current project you must implement either a project component or a startup activity extension.

If you need to dispose data after a project is closed you have to implement a component. If just the initialization is needed then a startup activity extension is enough.

Project components

A project component is similar to an application component. It is created when a new project is opened and disposed after the project is closed.

The current project is available by dependency injection as constructor parameter. You can add other application and/or project components as constructor parameters.

1
2
3
<!-- in plugin.xml -->
<postStartupActivity
    implementation="com.plugin.PluginStartupActivity" />
1
2
3
4
5
6
// Java class
public class MyPostStartupActivity implements StartupActivity {
    public void runActivity(@NotNull Project project, @NotNull MyApplicationComponent applicationComponent) {

    }
}
Project component implementation
MyProjectComponent.java
Sample code on github
ProjectComponent.java
Interface definition of a project component

Startup activity after a project was opened

If you would like to run some code after a project has been opened you could implement the postStartupActivity extension point. If your extension implements the DumbAware interface, then the runActivity method is run just after the project was opened. If your extension doees not implement that interface, then runActivity is delayed until the dumb mode is finished, i.e. as soon as the background indexing of IntelliJ is done.

Be careful though! I don’t see any guarantee that the execution is done in the background, so take extra care that you don’t block the whole application. IntelliJ’s own implementations of this extension point usually trigger some more background action or show a notification after the project has been opened.

1
2
<postStartupActivity
    implementation="com.plugin.PluginStartupActivity" />
1
2
3
4
5
public class MyPostStartupActivity implements StartupActivity {
    public void runActivity(@NotNull Project project) {

    }
}
Startup activity declaration in plugin.xml
MyPostStartupActivity.java
Sample code on github
StartupActivity.java
Interface definition of the StartupActivity extension
ProjectManagerImpl.java
IntelliJ’s implementation where all postStartupActivity extension points are executed

Project shutdown

Implement the disposeProject method of your ProjectComponent to execute some code after a project has been closed.