代码改变世界

如何获取Eclipse已经加载的插件

2012-04-29 16:22  chen.simon  阅读(784)  评论(0编辑  收藏  举报

其实不用这么麻烦,有很多很方便的方法

文章末尾介绍一种

背景:

在Eclipse插件开发,自动化构建产品的过程中,当产品构建好之后,如何得知所build的插件都是build的成功且是被成功加载的。点击Eclipse的/About Eclipse/Install Details/Plug-ins 然后能在这个页面所成功加载的插件.

 

 

 

但是要是开发规模仅是几个插件还是肉眼能区分的,如果插件个数很多就难弄了。遗憾的是这个页面不能导出 不能复制。只能肉眼看。当你开发的产品涉及自己开发的插件有上几百个的时候,就比较麻烦了。

 

解决办法

修改其源码,将这些插件名字写进文件。

如何修改

经搜索发现这个About页面是在org.eclipse.ui.workbench插件中实现的

找到相应的java类,做适当修改,重新编译,替换jar包中的class,以clean方式启,便能实现。

我们开发的产品对应 Eclipse 3.5.2

对应其源码在http://archive.eclipse.org/eclipse/downloads/drops/R-3.5.2-201002111343/eclipse-SDK-3.5.2-win32.zip

org.eclipse.ui.workbench插件 版本是 org.eclipse.ui.workbench_3.5.2.M20100113-0800.jar

其对应的源码包是org.eclipse.ui.workbench.source_3.5.2.M20100113-0800.jar

解压这个源码包导入进Eclipse

将org.eclipse.ui.workbench_3.5.2.M20100113-0800.jar中的plugin.xml文件放至工程中,将META-INF\MANIFEST.MF这个文件替换工程中相应文件

这样这个工程依赖其他插件的配置就ok了。如下图:

 

找到org.eclipse.ui.internal.about.AboutPluginsPage这个类

在 createControl(Composite parent) 方法中略作修改

将bundleInfos中获得的插件名称写入至log文件

修改如下

 1     public void createControl(Composite parent) {
 2         initializeDialogUnits(parent);
 3 
 4         // create a data object for each bundle, remove duplicates, and include
 5         // only resolved bundles (bug 65548)
 6         Map map = new HashMap();
 7         for (int i = 0; i < bundles.length; ++i) {
 8             AboutBundleData data = new AboutBundleData(bundles[i]);
 9             if (BundleUtility.isReady(data.getState())
10                     && !map.containsKey(data.getVersionedId())) {
11                 map.put(data.getVersionedId(), data);
12             }
13         }
14         bundleInfos = (AboutBundleData[]) map.values().toArray(
15                 new AboutBundleData[0]);
16         
17         /**
18          * 将Eclipse已经加载所有插件名称排序后写入log文件
19          * 以方便ci集成后check
20          */
21         List<String> allPluginsName = new ArrayList<String>(bundleInfos.length);
22         for(AboutBundleData aboutBundleInfo : bundleInfos) {
23             allPluginsName.add(aboutBundleInfo.getBundle().getSymbolicName());
24         }
25         Collections.sort(allPluginsName);
26         
27         StringBuilder sb = new StringBuilder();
28         for(String tempPluginName : allPluginsName) {
29             sb.append(tempPluginName);
30             sb.append("\n");
31         }
32         String eclipseInsPath = Platform.getInstallLocation().getURL().getPath();
33         String allPluginsNameLogFilePath = eclipseInsPath + "/allPlugins.log";
34         File allPluginsNameLogFile = new File(allPluginsNameLogFilePath);
35         try {
36             if(allPluginsNameLogFile.exists()) {
37                 allPluginsNameLogFile.delete();
38             }
39             Writer fWriter = new FileWriter(allPluginsNameLogFile);
40             BufferedWriter bfWriter = new BufferedWriter(fWriter);
41             bfWriter.write(sb.toString());
42             bfWriter.flush();
43             bfWriter.close();
44             fWriter.close();
45         } catch (IOException e) {
46             e.printStackTrace();
47         }
48         
49         
50         WorkbenchPlugin.class.getSigners();
51 
52         sashForm = new SashForm(parent, SWT.HORIZONTAL | SWT.SMOOTH);
53         FillLayout layout = new FillLayout();
54         sashForm.setLayout(layout);
55         layout.marginHeight = 0;
56         layout.marginWidth = 0;
57         GridData data = new GridData(GridData.FILL_BOTH);
58         sashForm.setLayoutData(data);
59 
60         Composite outer = createOuterComposite(sashForm);
61         PlatformUI.getWorkbench().getHelpSystem().setHelp(outer, helpContextId);
62 
63         if (message != null) {
64             Label label = new Label(outer, SWT.NONE);
65             label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
66             label.setFont(parent.getFont());
67             label.setText(message);
68         }
69 
70         createTable(outer);
71         setControl(outer);
72     }

 

代码修改好后,clean 工程使其重新编译,到bin\org\eclipse\ui\internal\about 找到AboutPluginsPage几个class(含其内部类编译出来的class)

关闭eclipse

替换掉org.eclipse.ui.workbench_3.5.2.M20100113-0800.jar包中相应的class文件

eclipse -clean 方式启动eclipse 即可。

 

 

//===========Update on 2012-09-15==============

 

后来发现其实不用这么麻烦 有很多很方便的方法

 

在eclipse的目录下   configuration/org.eclipse.equinox.simpleconfigurator 目录中有bundles.info 这个文件,这其中有记录被安装过的插件

信息类似  com.android.ide.eclipse.ddms,18.0.0.v201203301601-306762,plugins/com.android.ide.eclipse.ddms_18.0.0.v201203301601-306762.jar,4,false

这个文件其实明堂还是有点

 

包括如果你的插件id相同但是版本号不同

就可能出现加载不起来的情况

 

以及插件打包形式  jar换成目录的形式不起做用等等

 

 

 

还有一种方便的方法

shucheng发现的 就是在插件的activtor上做文章

 

--EOF--