get notified when a maven project is imported

Hi -

In my plugin I create and open a maven project in IDEA. 

I would like to do something when project has been successfully imported. As far as I can see, StartupManager.getInstance(project).registerPostStartupActivity etc get called to early. The modules are not yet available - ModuleManager.getInstance(project).findModuleByName(...) returns null.

Same with org.jetbrains.idea.maven.utils.MavenUtil#runWhenInitialized which looked more promising.

How can I know when a project is properly imported from maven and the modules are available?

thanks, jamie


How do you create and open the project?


I just create a dir, write some pom files to the dir, then call com.intellij.ide.impl.ProjectUtil#openOrImport



I just create a directory, write some pom files to the directory, then call com.intellij.ide.impl.ProjectUtil.openOrImport

Sorry if this comment is duplicated, my previous one is "Pending Approval" which I haven't seen before.


You can try the following:

MavenUtil.runWhenInitialized(openedProject, () -> {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(openedProject);
List<Module> modules = projectsManager.importProjects();

Awesome, thanks. Unfortunately the code you gave me in the previous comment returned immediately, and the modules were not available.

For now I settled for a "module added" listener.


@Vladislav Soroka How do I register that listener?

MessageBusConnection connection = myProject.getMessageBus().connect(myProject);
connection.subscribe(MavenImportListener.TOPIC, new MavenImportListener() {
public void importFinished(@NotNull Collection<MavenProject> importedProjects, @NotNull List<Module> newModules) {
// ...


See documentation of Messaging API at 


Usually, project is available as a method parameter and if I used the above mentioned method, then each time that method is invoked, a new Listener will be subscribed. Is there a sort of a central place (like plugin.xml) in code base where I can subscribe through a listener only once.


Please sign in to leave a comment.