How to add a JarDirectory via a Plugin?

Hi guys, I've read through the forums about how to add a Jar to the module via a plugin, but what I want to do is add a JarDirectory.

It seemed that a slight modification of the work others had done with adding a JAR would suffice, but everything I do fails to create a dependant Library in my module.

Here's the current state of my code:

                            String jarPath = "/opt/somedir/share/jars";
                            String jarUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, jarPath);
                            VirtualFile jarVirtualFile = VirtualFileManager.getInstance().findFileByUrl(jarUrl);

                            // Getting Module Root Model:
                            ModuleRootManager rootManager = ModuleRootManager.getInstance(mod);
                            ModifiableRootModel rootModel   = rootManager.getModifiableModel();
                            // Creating the new Libaray, And adding its jar:
                            Library myLibrary = rootModel.getModuleLibraryTable().createLibrary("NewLibrary");
                            Library.ModifiableModel libraryModel = myLibrary.getModifiableModel();
                            libraryModel.addJarDirectory(jarVirtualFile, true);
                            libraryModel.commit();
                            // Committing the Root Model to the new changes:
                            rootModel.commit();


If I change the jarPath to a JAR, and change "addJarDirectory" to "addRoot(jarVirtualFile, OrderRootType.CLASSES);" -- I get a single jar in a library.  What I need are ALL the jars in the directory.
I don't want to put this into a loop and create N new library entries for each jar I find, since the functionality is already here - and seems exposed.

First thing that I had to do which wasn't explained anywhere was call "application.runWriteAction()" to get anything to work at all (is this documented somewhere as well?)

Can anyone shed some light? Point me int he right direction? Maybe link an example or docs?

TIA

WG

Please sign in to leave a comment.