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

1
10 comments

How do you create and open the project?

0

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

 

0

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.

0

You can try the following:

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

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.

0

@Vladislav Soroka How do I register that listener?

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

 

See documentation of Messaging API at http://www.jetbrains.org/intellij/sdk/docs/reference_guide/messaging_infrastructure.html 

0

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.

0

Please sign in to leave a comment.