Monday, September 7, 2009

New Plug-in Wizard template: Can I add Import-Package programmatically?

Dear Planet Eclipse readers, please take notice of my problem with adding an Import-Package to the MANIFEST.MF using the Plug-in Wizard templating mechanism. Any suggestions and pointers very much appreciated! I'd really like to remove step 7 from the following tutorial:

Last Friday, the Bioclipse 2.1 development series moved to Eclipse 3.5, so I had to update the Bioclipse SDK too, which we developed earlier.

With a new Eclipse version also comes new screenshots to talk you through the process of setting up a new Bioclipse manager plugin.

Step 1
Right click in your workspace navigator, and choose New -> Project:



Step 2
And select to create a new Plug-in Project:



Step 3
Give a project name, such as net.bioclipse.xml:



Step 4
Tune the ID, Version, Name, and Provider to your liking:



Step 5
Then select Bioclipse Manager:



Step 6
The next wizard page is specific the the Bioclipse manager, and asks a manager namespace, which will be used as prefix in the JavaScript Console. For example, if I make the namespace xml, then I will type xml.someMethod() inside the JavaScript. The default manager name is typically OK by default:

Then click Finish and let Eclipse set up the new project.

Step 7
Because I have not figured out yet how to add Import-Package to the MANIFEST.MF programmatically, you will have to do this manually. Add the last line of the next screenshot to the MANIFEST.MF of your new plugin:



Update: I found a hack to add the Import-Package programmatically, by overwriting the execute(IProject project, IPluginModelBase model, IProgressMonitor monitor) in the Template class.

8 comments:

  1. regarding adding Import-Package, I believe you can do this by extending NewPluginTemplateWizard.getImportPackages()

    ReplyDelete
  2. I'm adding Import-Package to MANIFEST.MF programmatically as part of my oaw - generator - workflow.
    this week I have no time - please remember me nex t week (ekke_at_ekkes-corner.org) then I can publish my solution - perhaps it helps.
    ekke

    ReplyDelete
  3. Jacek is correct, use the getImportPackages() method... it's a bit wonky but there are example templates in the org.eclipse.pde.ui.templates plug-in

    ReplyDelete
  4. @Chris and @Jacek:

    First of all, thanx very much for your feedback... I'm trying to get it working, but need a bit more info I think...

    We have a class:

    public class ManagerWizard extends NewPluginTemplateWizard {}

    to which I just added:

    @Override
    public String[] getImportPackages() {
    String[] superPkg = super.getImportPackages();
    String[] pkgs = new String[superPkg.length+1];
    System.arraycopy(superPkg, 0, pkgs, 0, superPkg.length);
    pkgs[pkgs.length-1] = "org.apache.log4j";
    return pkgs;
    }

    Our plugin.xml for this class looks like:

    <extension
    point="org.eclipse.pde.ui.pluginContent">
    <wizard
    class="net.bioclipse.sdk.pdewizard.ManagerWizard"
    icon="icons/bioclipse_16.png"
    id="net.bioclipse.sdk.template.wizard"
    java="true"
    name="Bioclipse Manager Wizard"
    pureOSGi="false"
    rcp="false"
    requiresActivator="false"
    ui-content="true">
    </wizard>
    </extension>

    Does that look about right? As it does not add the imports line... :(

    ReplyDelete
  5. Looks fine, looking at the HelloOSGiServiceWizard...

    public String[] getImportPackages() {
    return new String[] {"org.osgi.framework;version=\"1.3.0\"", "org.osgi.util.tracker;version=\"1.3.1\""}; //$NON-NLS-1$ //$NON-NLS-2$
    }

    ReplyDelete
  6. No, I just cannot get it to work... I actually have one more problems... the packages that the templates are not exported with Export-Package: in the MANIFEST.MF either... :(

    Quite depressing... I'll try to step-by-debug-step through the whole performFinish() to learn a bit more on why this is failing for me... :(

    ReplyDelete
  7. OK, I found a dirty hack which is using deprecated APIs, but at least works: adding the following to the Template class:

    @Override
    public void execute(IProject project, IPluginModelBase model,
    IProgressMonitor monitor) throws CoreException {
    IPluginBase pluginBase = model.createPluginBase();
    if (pluginBase instanceof BundlePluginBase) {
    IBundle bundle = ((BundlePluginBase) pluginBase).getBundle();
    bundle.setHeader("Import-Package", "org.apache.log4j");
    model.createPluginBase().getId();
    String packageName = getFormattedPackageName(
    model.getPluginBase().getId()
    );
    bundle.setHeader(
    "Export-Package",
    packageName + ", " +
    packageName + ".business"
    );
    }
    super.execute(project, model, monitor);
    }

    ReplyDelete
  8. Really.... a cool Plug-in Wizard template for Bioclipse. kudos :)

    ReplyDelete