简体   繁体   English

将嵌入式插件jar文件添加到项目

[英]Add embedded plugin jar file to a project

I need to add some jar libraries to a project with my plugin by modifying classpath entry. 我需要通过修改类路径条目将一些jar库添加到带有我的插件的项目中。 But i can't find a way to do that. 但是我找不到办法。 I tried to nested jar lib inside the lib folder of my plugin but i can't access them when the plugin is packaged as Jar and deploy on an eclipse IDE. 我试图将jar lib嵌套在插件的lib文件夹中,但是当插件打包为Jar并部署在Eclipse IDE上时,我无法访问它们。 Any Solution? 有什么办法吗? What is the right way to do this? 什么是正确的方法?

This is the code that add lib to the target project : 这是将lib添加到目标项目的代码:

private void addLocalLibrary(IProject parent, String name, boolean source, boolean javadoc)
    throws CoreException, URISyntaxException, IOException {
IClasspathEntry[] oldEntries = this.javaProject.getRawClasspath();
// +1 for our src/main/java entry
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);

String pluginDir = new File(Activator.getPluginDir()).getAbsolutePath().replace("file:", "");
IPackageFragmentRoot lib = this.javaProject.getPackageFragmentRoot(pluginDir + "/" + name);
IPackageFragmentRoot src = null;
if (source) {
    src = this.javaProject.getPackageFragmentRoot((pluginDir + "/" + name).replace(".jar", "-sources.jar"));
}
IClasspathAttribute atts[] = null;
if (javadoc) {
    IPackageFragmentRoot doc = this.javaProject
        .getPackageFragmentRoot((pluginDir + "/" + name).replace(".jar", "-javadoc.jar"));
    atts = new IClasspathAttribute[] {
        JavaCore.newClasspathAttribute("javadoc_location", doc.getPath().toString()), };
}
IPath srcPath = src == null ? null : src.getPath();
newEntries[oldEntries.length] = JavaCore.newLibraryEntry(lib.getPath(), srcPath, null, null, atts, true);
this.javaProject.setRawClasspath(newEntries, null);
}

You can create a plugin from the existing library jar and add it to the dependencies of the plugin where you want to use the library code. 您可以从现有的库jar中创建一个插件,并将其添加到要使用库代码的插件的依赖项中。

To do so, use the Plug-in from existing JAR archives wizard , which is available under File > New > Project... > Plug-in Development > Plug-in from existing JAR archive . 为此,请使用“ 来自现有JAR存档插件”向导 ,该向导位于“ 文件”>“新建”>“项目...”>“插件开发”>“来自现有JAR存档的插件”下

This way you can wrap existing jars in a plugin project. 这样,您可以将现有的jar包装在插件项目中。 You can also use this plugin as the base for your own code. 您也可以将此插件用作自己代码的基础。 If you want to package as a single jar, make sure to unzip the library jar into the plugin. 如果要打包为单个jar,请确保将库jar解压缩到插件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM