How do I add a library to project, and reuse them in all the required modules ?

Answered

I'm having multiple modules and I'm adding the library to a module using the below given snippet to each module one by one. 

val localFileSystem = LocalFileSystem.getInstance()
val moduleRootManager = ModuleRootManager.getInstance(module)
val modifiableRootModel = moduleRootManager.modifiableModel
val library = modifiableRootModel.moduleLibraryTable.createLibrary("BUILD_JARS")
val virtualFile = localFileSystem.findFileByIoFile(file)
if(virtualFile != null){
modifiableModel.addJarDirectory(virtualFile,false)
}
modifiableModel.commit()

If I was able to add a library to a project instead of module and reuse the same library when adding to modules it would make it better. Please help me find the right class to use to add a library to project. 

Thanks.

0
1 comment

com.intellij.openapi.roots.libraries.LibraryTable on Project-level can be obtained via com.intellij.openapi.roots.libraries.LibraryTablesRegistrar#getLibraryTable(com.intellij.openapi.project.Project)

0

Please sign in to leave a comment.