我有一个eclipse插件,它提供了一个需要jsdt javaScriptNature的项目性质。我现在想以编程方式将插件中包含的javascript库添加到项目的includepath中。有没有办法可以做到这一点?
我读了一些关于JsGlobalScopeContainer和JsGlobalScopeContainerInitializer的内容并试了一下,但这看起来很混乱。我只是想从我的插件中添加一个包含一些.js文件的库。我无法理解这个概念。
这是我到目前为止所提出的:
IJavaScriptProject jsProj = JavaScriptCore.create(p);
Path pa = new Path("/src/de/otris/eclipse/portalscripting/psLibrary/library.js");
IIncludePathEntry entry = JavaScriptCore.newProjectEntry(pa);
IIncludePathEntry[] ipaths = jsProj.getRawIncludepath();
IIncludePathEntry[] newpaths = new IIncludePathEntry[ipaths.length +1];
System.arraycopy(ipaths, 0, newpaths, 0, ipaths.length);
newpaths[ipaths.length] = entry;
jsProj.setRawIncludepath(newpaths, null);
答案 0 :(得分:1)
我终于找到了一种直接从我的插件添加我的库的方法。虽然尤金的回答并没有错,但缺乏一些解释。我将尝试展示如何做到这一点。
如果要添加包含多个文件的库,可以这样做:
1。创建一个扩展JsGlobalScopeContainerInitializer的类
有一些非常令人困惑的教程(包括eclipse wiki上的教程)最初让人更难理解这一点。我提出了以下内容:
[... package and imports ommited ...]
public class LibInitializer extends JsGlobalScopeContainerInitializer {
private static final String LIBRARY_ID = "com.testvendor.testplugin.library";
public IPath getPath() {
return new Path(LIBRARY_ID);
}
@Override
public LibraryLocation getLibraryLocation() {
return null;
}
@Override
public String getDescription() {
return "Test Library";
}
@Override
public String getDescription(IPath containerPath, IJavaScriptProject project) {
return getDescription();
}
@Override
public IIncludePathEntry[] getIncludepathEntries() {
try {
//get the Bundle object of the plugin
Bundle bundle = Platform.getBundle("com.testvendor.testplugin");
//get the java.io.File object corresponding to the root of the bundles installation directory
File bundleFile = FileLocator.getBundleFile(bundle);
//add the location pointing to the library relative to that bundle root
File libraryLocation = new File(bundleFile, "bin/com/testvendor/testplugin/library/");
//create a Path object from it
IPath pa = new Path(libraryLocation.getAbsolutePath());
/* create an IIncludePathEntry of the type "library" from this path
my library only contains one folder (for now) so this is it */
IIncludePathEntry entry = JavaScriptCore.newLibraryEntry(pa, pa, pa);
//put the entry (or entries if you had more) into an array and return
IIncludePathEntry[] entries = {entry};
return entries;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
最有趣的部分是 getIncludepathEntries()方法,其中将从容器中检索实际条目。由于IIncludePathEntry不能使用“file://”伪协议的URL,因此Eugene建议的方法“toFileURL”将不起作用。
2. 为JSGlobalScope ...扩展点贡献扩展
告诉包含id为 com.testvendor.testplugin.library 的容器条目的项目,最简单的方法是为扩展点做贡献* org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer * *像这样:
<extension point="org.eclipse.wst.jsdt.core.JsGlobalScopeContainerInitializer">
<JsGlobalScopeContainerInitializer
id="com.testvendor.testplugin.library"
class="com.testvendor.testplugin.library.LibInitializer"/>
</extension>
其中课程是指步骤1中的JsGlobalScopeContainerInitializer。
3. 将IIncludePathEntry添加到项目
IJavaScriptProject jsProj = ... get your project object from somewhere ...
//create an instance of the container from step 1.
JsGlobalScopeContainerInitializer container = new LibInitializer();
//create an includepath entry refering to the container
IIncludePathEntry entry = JavaScriptCore.newContainerEntry(container.getPath());
IIncludePathEntry[] ipaths = jsProj.getRawIncludepath();
IIncludePathEntry[] newpaths = new IIncludePathEntry[ipaths.length +1];
System.arraycopy(ipaths, 0, newpaths, 0, ipaths.length);
//add the new entry
newPaths[ipaths.length] = enty;
// set the new includepath to the project
jsProj.setRawIncludepath(newpaths, null);
如果您现在幸运,您的JavaScript资源中将包含一个库条目,其中包含您使用ContainerIntitializer添加的库文件夹中包含的所有JavaScript对象和类。所有这些对象和类都将在代码完成建议中提供。
我希望这可以防止其他人在一个真正可能比现在更简单的话题上花费数小时的挫折感。
答案 1 :(得分:-1)
要设置项目包含路径,您应该使用IJavaScriptProject::setRawIncludepath方法之一。 IIncludePathEntries是通过调用与条目类型对应的JavaScriptCore::new*Entry方法创建的。