Adding / Removing Project Libraries
How do you add / remove project libraries and associate them with modules? I didn't see it on the Project or Module interfaces...
What I want to do is to look up the jar files using Ivy and then create the libraries in the project. For the modules that have dependencies on these libraries, I need to turn those on...
Please sign in to leave a comment.
i had the same problem with the maven plugin ...
i came up with the following (with help from the nice guys here):
this snippets are a little out of context, but i hope you get an idea
(and i hope that this is still the way to go) :)
private Library createLibForLevel(final ModuleRootManager rootManager,
final String libName,
final Dependency dep, final Object
level) { String jarFile = dep.getPath();
jarFile = jarFile.replace('
', '/');
String urlString =
VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, jarFile) + JarFileSystem.JAR_SEPARATOR; VirtualFile jarVirtualFile =
VirtualFileManager.getInstance().findFileByUrl(urlString); Library newLib = null;
if (level == Dependencies.MODULE_LEVEL_LIB) {
ModifiableRootModel modifiableModel =
rootManager.getModifiableModel(); newLib = createLib(libName, jarVirtualFile,
modifiableModel.getModuleLibraryTable()); modifiableModel.commit();
} else if (level == Dependencies.PROJECT_LEVEL_LIB) {
LibraryTable table = (LibraryTable)
fProject.getComponent(LibraryTable.class); newLib = createLib(libName, jarVirtualFile, table);
ModifiableRootModel modifiableModel =
rootManager.getModifiableModel(); modifiableModel.addLibraryEntry(newLib);
modifiableModel.commit();
}
return newLib;
}
private Library createLib(final String libName, final VirtualFile
jarVirtualFile, final LibraryTable table) { Library res = table.getLibraryByName(libName);
if (res == null) {
res = table.createLibrary(libName);
Library.ModifiableModel libraryModel = res.getModifiableModel();
libraryModel.addRoot(jarVirtualFile, OrderRootType.CLASSES);
libraryModel.commit();
}
return res;
}
cK
Thanks very much Christian! Your code is definitely helping me on my way...
Now, however, I've run into something that doesn't give a good error message. I implemented your code (with some modifications for my case), but I got assertion failed messages telling me that write operations had to happen in an action run from application.runWriteAction() with a Runnable. Ok, fine, I pulled this code which creates the library records out into a Runnable inner class. Now I get this:
(VirtualFilePointerImpl.java:11) at com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl.create(VirtualFilePointerManagerImpl.java:34) at com.intellij.openapi.vfs.impl.VirtualFilePointerContainerImpl$DefaultFactory.create(VirtualFilePointerContainerImpl.java:4) at com.intellij.openapi.vfs.impl.VirtualFilePointerContainerImpl.add(VirtualFilePointerContainerImpl.java:5) at com.intellij.openapi.roots.impl.libraries.LibraryImpl.addRoot(LibraryImpl.java:41) at com.opensymphony.ivyclasspath.IvyModuleComponent$LibraryWriteAction.createLib(IvyModuleComponent.java:177) at com.opensymphony.ivyclasspath.IvyModuleComponent$LibraryWriteAction.run(IvyModuleComponent.java:164) at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:28) at com.opensymphony.ivyclasspath.IvyModuleComponent.addLibrary(IvyModuleComponent.java:141) at com.opensymphony.ivyclasspath.IvyModuleComponent.refeshIvy(IvyModuleComponent.java:89) at com.opensymphony.ivyclasspath.IvyConfigurable.refreshIvy(IvyConfigurable.java:147) at com.opensymphony.ivyclasspath.IvyConfigurable.apply(IvyConfigurable.java:110) at com.intellij.openapi.options.ex.ProjectConfigurableWrapper.apply(ProjectConfigurableWrapper.java:15) at com.intellij.openapi.options.ex.SingleConfigurableEditor.doOKAction(SingleConfigurableEditor.java:11) at com.intellij.openapi.ui.DialogWrapper$OkAction.actionPerformed(DialogWrapper.java:837) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3093) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1774) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:18) at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:41) at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:16) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153) at java.awt.Dialog$1.run(Dialog.java:515) at java.awt.Dialog.show(Dialog.java:536) at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl$MyDialog.show(DialogWrapperPeerImpl.java:18) at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:128) at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:734) at com.intellij.openapi.options.ex.ControlPanelSettingsEditor.b(ControlPanelSettingsEditor.java:81) at com.intellij.openapi.options.ex.ControlPanelSettingsEditor.access$900(ControlPanelSettingsEditor.java:3) at com.intellij.openapi.options.ex.ControlPanelSettingsEditor$MyActionButton$1.mouseReleased(ControlPanelSettingsEditor.java:5) at java.awt.Component.processMouseEvent(Component.java:5488) at javax.swing.JComponent.processMouseEvent(JComponent.java:3093) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1774) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:18) at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:41) at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:16) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153) at java.awt.Dialog$1.run(Dialog.java:515) at java.awt.Dialog.show(Dialog.java:536) at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl$MyDialog.show(DialogWrapperPeerImpl.java:18) at com.intellij.openapi.ui.impl.DialogWrapperPeerImpl.show(DialogWrapperPeerImpl.java:128) at com.intellij.openapi.ui.DialogWrapper.show(DialogWrapper.java:734) at com.intellij.ide.actions.ShowSettingsUtilImpl.showControlPanelOptions(ShowSettingsUtilImpl.java:44) at com.intellij.ide.actions.ShowSettingsUtilImpl.showSettingsDialog(ShowSettingsUtilImpl.java:38) at com.intellij.ide.actions.ShowSettingsAction.actionPerformed(ShowSettingsAction.java:1) at com.intellij.openapi.actionSystem.impl.ActionButton.performAction(ActionButton.java:103) at com.intellij.openapi.actionSystem.impl.ActionButton.processMouseEvent(ActionButton.java:21) at java.awt.Component.processEvent(Component.java:5253) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822) at java.awt.Container.dispatchEventImpl(Container.java:2010) at java.awt.Window.dispatchEventImpl(Window.java:1774) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:18) at com.intellij.ide.IdeEventQueue.a(IdeEventQueue.java:41) at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:16) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110) [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - Assertion failed [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - IntelliJ IDEA (Irida) #3339 Build #3339 [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - JDK: 1.5.0_03 [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - VM: Java HotSpot(TM) Client VM [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - Vendor: Sun Microsystems Inc. [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - OS: Windows XP [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - Last Action: ShowSettings [ 154937] ERROR - pl.SmartVirtualFilePointerImpl - Assertion failed ]]>
Does anyone have any idea what this means?
ping.... bringing this back to the top... Anyone have any idea how to get around this problem?
JetBrains,
Any chance of giving Jason some suggestions?
A lot of us would like him to build the Ivy plugin ;)
Taras Tielkes wrote:
Very much would like to see this plugin. And if it could also set
dependencies to "export" to an exploded dir that'd be cool.
What would you guys want in such an Ivy plugin? ( note: followup-to is
set to the plugins group... to which I'm also cross-posting this...)
Personally:
.ivy file editor - probaby just a module level "options form" to
set the various ivy details.
Path resolver
Jar exporter
Repository browser?
Mark