How to list module dependencies?

Answered

I'm trying to attach a project library to a module if it's not yet attached. Like this:

```

for (Module module : ModuleManager.getInstance(project).getModules()) {
final ModifiableRootModel modifiableModel = modelsProvider.getModuleModifiableModel(module);
Library libraryByName = modifiableModel.getModuleLibraryTable().getLibraryByName(libraryName);

if (libraryByName != null) // this unfortunately never happens :-(
continue;

modifiableModel.addLibraryEntry(library);
modelsProvider.commitModuleModifiableModel(modifiableModel);
}
```

However ` modifiableModel.getModuleLibraryTable().getLibraryByName(libraryName)` always returns `null`, and also `modifiableModel.getModuleLibraryTable().getLibraries()` is always just returning an empty array. 

But actually the library is already attached after the loop was run once and I end up having multiple copies of my libraries attached to each module:

Is there a more correct way to list attached module dependencies?

2 comments

ModuleLibraryTable contains only module-level libraries, i.e. libraries defined in that module, it doesn't include project libraries added to the module dependencies. If you want to find a dependency on a project library you either need to call ModuleRootModel#getOrderEntries and check LibraryOrderEntry's in the returned array, or use more convenient OrderEnumerator class: OrderEnumerator.orderEntries(module).forEachLibrary will process all libraries from the module dependencies list.

0

Thanks Nikolay, this was exactly what I was looking for! :-)

0

Please sign in to leave a comment.