Adding global library to module dependencies

As a part of new module creation/initialization process, I need to add one of the existing global libraries to the module dependencies. After some research and going through a number of existing posts here, I came up with the following code:

final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();

String libraryName = "MyExistingPredefinedLib";
LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTableByLevel(LibraryTablesRegistrar.APPLICATION_LEVEL, module.getProject());

Library[] libs = table.getLibraries();
for (final Library lib : libs) {
if (lib.getName().equalsIgnoreCase(libraryName)) {

ApplicationManager.getApplication().runWriteAction(() ->

For some reason, it still doesn't work. It doesn't throw any exception or produce any warning in the logs, but it doesn't do anything. I ran it in the debugger, I can clearly see that it finds the correct global library and the code inside write action is invoked, but I don't see any library dependencies in the module settings dialog. What am I missing here?


LibraryTablesRegistrar#getLibraryTableByLevel returns only custom library tables. In order to get the global library table you need to use LibraryTablesRegistrar#getLibraryTable() method. I've committed javadoc comments for LibraryTableRegistrar describing that.

'lib.getModifiableModel().commit()' line is useless. There's no need to create a modifiable model for the library itself if you don't modify it.

Also as described in javadoc for ModuleRootManager#getModifiableModel its result must be even committed or disposed. If a global library with the specified name doesn't exist 'model' won't be disposed and this will lead to memory leak. To fix this you can simply call 'ModuleRootManager#getModifiableModel' just before adding a library to the model.


Thanks, Nikolay, it does make sense, but I think my problem is elsewhere. My code does find the predefined global library and adds it to the model. However, since my projects are Maven-based, it seems like the MavenModuleBuilder overrides all module libraries and replaces them with libraries defined in pom.xml - could this be the case? If yes, is there any way to tell the maven module builder to add my library after all jars from the pom are added to the module dependencies?



And just to add to this mystery - it happens when I add a new module to the existing project. When I create a new project from scratch, everything works as expected, I see my library in the module dependencies. But if I add a new module to the existing project, I see that my code is invoked, the library is added to the model and the model is committed - but then for some reason the library entry disappears and gets replaced by pom dependencies. 


OK, sorry for spamming, I think I found the solution - I simply added a module builder listener and moved the code above to the moduleCreated() method - and this solves my problem, since it is invoked after module dependencies are created.


Please sign in to leave a comment.