我正在尝试为Netbeans 8插件做出贡献,该插件目前已被破坏。该插件包含一个"添加支持"菜单项,除其他外,如果项目是ANT项目,则应该在项目类路径中添加一个条目。目前,它通过手动重写项目属性文件来完成此操作。在某些情况下失败,例如如果项目有专用的库文件夹。 我设法编写了一个修复程序,它使用ProjectClassExtender API类将库添加到类中,但这是一个不推荐使用的API。我已经尝试了几种通过替换ProjectClassPathModifier工作的方法,但我无法获得正确的参数组合 - 我总是得到一个不支持的操作异常或类似的。有人可以帮忙吗? 下面的代码浓缩了我尝试过的三种方法:
FileObject projectRoot = project.getProjectDirectory();
Sources sources = ProjectUtils.getSources(project);
SourceGroup sourceGroup = sources.getSourceGroups(Sources.TYPE_GENERIC)[0];
FileObject sgRoot = sourceGroup.getRootFolder();
Library lib = libraryManager.getLibrary(LIB_NAME);
ProjectClassPathModifier pcpm = projectLookup.lookup(ProjectClassPathModifier.class);
ProjectClassPathExtender pcpe = pcpm.extenderForModifier(project);
pcpe.addLibrary(lib); // Works but deprecated
pcpm.addLibraries(new Library[]{lib}, projectRoot, ClassPath.COMPILE); // Fails regardless
pcpm.addLibraries(new Library[]{lib}, sgRoot, ClassPath.COMPILE); // Fails regardless
使用projectRoot的调用会出现此错误:
Java.lang.UnsupportedOperationException: Project in C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication16 of class org.netbeans.modules.java.j2seproject.J2SEProject has a ProjectClassPathModifierImplementation but it will not handle classpath/compile for C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication16 extensible source groups: C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication16\src
at org.netbeans.api.java.project.classpath.ProjectClassPathModifier.findExtensible(ProjectClassPathModifier.java:388)
at org.netbeans.api.java.project.classpath.ProjectClassPathModifier.addLibraries(ProjectClassPathModifier.java:93)
at org.nemesis.antlr.v4.netbeans.v8.project.AntBasedProject.addPropertiesToProject(AntBasedProject.java:615)
at org.nemesis.antlr.v4.netbeans.v8.project.AntBasedProject.addANTLRSupport(AntBasedProject.java:122)
at org.nemesis.antlr.v4.netbeans.v8.project.action.AddANTLRSupport.actionPerformed(AddANTLRSupport.java:134)
[catch] at org.openide.awt.InjectorExactlyOne.actionPerformed(InjectorExactlyOne.java:78)
at org.openide.awt.ContextAction$Performer.actionPerformed(ContextAction.java:226)
at org.openide.awt.ContextManager.actionPerformed(ContextManager.java:260)
at org.openide.awt.ContextAction.actionPerformed(ContextAction.java:109)
at org.openide.util.actions.ActionInvoker$1.run(ActionInvoker.java:93)
at org.openide.util.actions.ActionInvoker.doPerformAction(ActionInvoker.java:116)
at org.openide.util.actions.ActionInvoker.invokeAction(ActionInvoker.java:99)
at org.openide.awt.GeneralAction$BaseDelAction.actionPerformed(GeneralAction.java:234)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:842)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:886)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
使用sgRoot进行的调用给出了类似的错误:
java.lang.UnsupportedOperationException: Project in C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication17 of class org.netbeans.modules.java.j2seproject.J2SEProject has a ProjectClassPathModifierImplementation but it will not handle classpath/compile for C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication17 extensible source groups: C:\Users\sparry\ownCloud\development\NetbeansProjects\JavaApplication17\src
at org.netbeans.api.java.project.classpath.ProjectClassPathModifier.findExtensible(ProjectClassPathModifier.java:388)
at org.netbeans.api.java.project.classpath.ProjectClassPathModifier.addLibraries(ProjectClassPathModifier.java:93)
at org.nemesis.antlr.v4.netbeans.v8.project.AntBasedProject.addPropertiesToProject(AntBasedProject.java:616)
at org.nemesis.antlr.v4.netbeans.v8.project.AntBasedProject.addANTLRSupport(AntBasedProject.java:122)
at org.nemesis.antlr.v4.netbeans.v8.project.action.AddANTLRSupport.actionPerformed(AddANTLRSupport.java:134)
[catch] at org.openide.awt.InjectorExactlyOne.actionPerformed(InjectorExactlyOne.java:78)
at org.openide.awt.ContextAction$Performer.actionPerformed(ContextAction.java:226)
at org.openide.awt.ContextManager.actionPerformed(ContextManager.java:260)
at org.openide.awt.ContextAction.actionPerformed(ContextAction.java:109)
at org.openide.util.actions.ActionInvoker$1.run(ActionInvoker.java:93)
at org.openide.util.actions.ActionInvoker.doPerformAction(ActionInvoker.java:116)
at org.openide.util.actions.ActionInvoker.invokeAction(ActionInvoker.java:99)
at org.openide.awt.GeneralAction$BaseDelAction.actionPerformed(GeneralAction.java:234)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:842)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:886)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:1)
我想我自己已经解决了问题和解决方案:
Sources sources = ProjectUtils.getSources(project);
SourceGroup sourceGroup = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)[0];
FileObject sgRoot = sourceGroup.getRootFolder();
Library lib = libraryManager.getLibrary(LIB_NAME);
ProjectClassPathModifier pcpm = projectLookup.lookup(ProjectClassPathModifier.class);
pcpm.addLibraries(new Library[]{lib}, sgRoot, ClassPath.COMPILE); // works!
关键是使用JavaProjectConstants.SOURCES_TYPE_JAVA
来源类型,而不是Sources.TYPE_GENERIC
。 Generic返回项目主文件夹,addLibraries调用需要java源文件夹。在大多数项目中,这是src文件夹。