公司有个需求,需要在eclipse插件中获取eclipse配置的tomcat的位置信息,于是我开始研究。
方式一:Platfrom.getPlugin()获取Preferences对象 (不可行)
Plugin plugin = org.eclipse.core.runtime.Platform.getPlugin("plugin_hello");
Preferences prefs = plugin.getPluginPreferences();
该方式不可行,Platfrom.getPlugin()方法已经过期了。另外运行了下,也无法获取到信息
方式二: 通过IPreferencesService来获取
IPreferencesService service = Platform.getPreferencesService();
Preferences instanceNode =
InstanceScope.INSTANCE.getNode("org.eclipse.jst.server.tomcat.core");
try {
String[] keyArray = instanceNode.keys();
for(String key : keyArray) {
System.out.println(key + " -----> " + instanceNode.get(key, null));
}
} catch (BackingStoreException e) {
e.printStackTrace();
}
以上会在InstanceScope中进行查找,如果不确定在哪个Scope中,可以使用多个Scope进行查找
IPreferencesService ips = Platform.getPreferencesService();
IScopeContext[] contexts = new IScopeContext[] {InstanceScope.INSTANCE,
ConfigurationScope.INSTANCE, DefaultScope.INSTANCE};
String val1 = ips.getString("org.eclipse.wst.server.core", "runtimes", "abcdc",
contexts);
String val2 = ips.getString("org.eclipse.jst.server.tomcat.core",
"locationorg.eclipse.jst.server.tomcat.runtime.85", "abcdc", contexts);
System.out.println(val1);
System.out.println(val2);
或者这样:
IPreferencesService ips = Platform.getPreferencesService();
IScopeContext[] contexts = new IScopeContext[] {InstanceScope.INSTANCE,
ConfigurationScope.INSTANCE, DefaultScope.INSTANCE};
for (int i = 0; i < contexts.length; ++i) {
value = contexts[i].getNode("org.eclipse.jst.server.tomcat.core")
.get("locationorg.eclipse.jst.server.tomcat.runtime.85", null);
if (value != null) {
value = value.trim();
if (!value.isEmpty()) {
break;
}
}
}
也可以通过构建 ScopedPreferenceStore对象来获取:
IPreferenceStore prefs2 = new ScopedPreferenceStore(InstanceScope.INSTANCE,
"org.eclipse.jst.server.tomcat.core");
String v2 = prefs2.getString("locationorg.eclipse.jst.server.tomcat.runtime.85");
低级的大坑:
一开始运行的时候一直无法获取到配置的值,调试很久,后面才醒悟,eclipse中调试插件,会在当前eclipse工作空间下独立生产一个runtime-EclipseApplication的插件运行环境的工作空间,这个独立的instance,没有配置tomcat,所以一直无法获取到值。我没有意识到我自己的开发eclipse工作空间和调试插件生成的插件运行环境工作空间是2个独立个体。
Scopes说明:
There are 4 preference scopes, derived from IScopeContext:
- project - ProjectScope /project/$(projectName)/$(qualifier)
- instance (workspace) - InstanceScope.INSTANCE /instance/$(qualifier)
- configuration (Eclipse) - ConfigurationScope.INSTANCE /configuration/$(qualifier)
- default (plug-in or feature) - DefaultScope.INSTANCE /default/$(qualifier)
The default preference search look-up order as defined by the platform is: project, instance, configuration, default.
持久化(Persistent stores)
preference配置信息持久化到可读写的文件中
- project(项目级)
- instance (工作空间级workspace)
- configuration (Eclipse全局)
Project store
The project preferences are stored in a file below the project .settings folder:
project/.settings/<pluginId>.prefs
Instance (workspace) store
The workspace preferences are stored is in a separate file named after the plug-in ID:
workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/<pluginId>.prefs
Configuration (Eclipse) store
The Eclipse preferences are stored is in a separate file named after the plug-in ID:
eclipse/configuration/.settings/<pluginId>.prefs
Defaults
Defaults are preference values used to initialise various variables.
Defaults are loaded into internal structures during Eclipse init and plug-in start-up, from several sources (see org.eclipse.core.internal.preferences.DefaultPreferences):
- command line -pluginCustomization (highest priority)
- product preferenceCustomization.ini
- plug-in preferences.ini
- AbstractPreferenceInitializer (lowest priority)
附录文章参考:
https://gnu-mcu-eclipse.github.io/developer/eclipse/runtime-preferences/