简体   繁体   中英

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. 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. Any Solution? What is the right way to do this?

This is the code that add lib to the target project :

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.

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 .

This way you can wrap existing jars in a plugin project. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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