eclipse-plugineclipse-rcprcpeclipse-pde

Override the dependencies added during running a project as an Eclipse Application


I am trying to write a custom launch configuration while running a plugin project as an eclipse application. I have to run the plugin with limited dependencies. Is it possible to override methods in org.eclipse.pde.launching.EclipseApplicationLaunchConfiguration ? If yes then how do I do it ?


Solution

  • To write a custom configuration file

    1. Extend the class org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut
    2. Override the method createLaunchConfiguration
    3. Invoking super.createLaunchConfiguration(element) will return a ILaunchConfigurationWorkingCopy
    4. Use the copy and set your own attributes that have to be modified
    5. Attributes can be found in IPDELauncherConstants

    Eclipse by default runs all the projects found in the workspace. This behavior can be modified by using the configuration created and overriding it with custom configuration.

    public class LaunchShortcut extends JUnitLaunchShortcut {
    
    class PluginModelNameBuffer {
    
        private List<String> nameList;
    
        PluginModelNameBuffer() {
            super();
            this.nameList = new ArrayList<>();
        }
    
        void add(final IPluginModelBase model) {
            this.nameList.add(getPluginName(model));
        }
    
        private String getPluginName(final IPluginModelBase model) {
            IPluginBase base = model.getPluginBase();
            String id = base.getId();
            StringBuilder buffer = new StringBuilder(id);
    
            ModelEntry entry = PluginRegistry.findEntry(id);
            if ((entry != null) && (entry.getActiveModels().length > 1)) {
                buffer.append('*');
                buffer.append(model.getPluginBase().getVersion());
            }
            return buffer.toString();
        }
    
        @Override
        public String toString() {
            Collections.sort(this.nameList);
            StringBuilder result = new StringBuilder();
            for (String name : this.nameList) {
                if (result.length() > 0) {
                    result.append(',');
                }
                result.append(name);
            }
    
            if (result.length() == 0) {
                return null;
            }
    
            return result.toString();
        }
    }
    
    @Override
    protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(final IJavaElement element)
            throws CoreException {
        ILaunchConfigurationWorkingCopy configuration = super.createLaunchConfiguration(element);
        configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "memory");
        configuration.setAttribute(IPDELauncherConstants.USE_PRODUCT, false);
        configuration.setAttribute(IPDELauncherConstants.USE_DEFAULT, false);
        configuration.setAttribute(IPDELauncherConstants.AUTOMATIC_ADD, false);
        addDependencies(configuration);
    
        return configuration;
    }
    
    @SuppressWarnings("restriction")
    private void addDependencies(final ILaunchConfigurationWorkingCopy configuration) throws CoreException {
        PluginModelNameBuffer wBuffer = new PluginModelNameBuffer();
        PluginModelNameBuffer tBuffer = new PluginModelNameBuffer();
        Set<IPluginModelBase> addedModels = new HashSet<>();
    
        String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IPluginModelBase model = PluginRegistry.findModel(project);
        wBuffer.add(model);
        addedModels.add(model);
    
        IPluginModelBase[] externalModels = PluginRegistry.getExternalModels();
        for (IPluginModelBase externalModel : externalModels) {
            String id = externalModel.getPluginBase().getId();
            if (id != null) {
                switch (id) {
                case "org.eclipse.ui.ide.application":
                case "org.eclipse.equinox.ds":
                case "org.eclipse.equinox.event":
                    tBuffer.add(externalModel);
                    addedModels.add(externalModel);
                    break;
                default:
                    break;
                }
            }
        }
    
        TreeSet<String> checkedWorkspace = new TreeSet<>();
        IPluginModelBase[] workspaceModels = PluginRegistry.getWorkspaceModels();
        for (IPluginModelBase workspaceModel : workspaceModels) {
            checkedWorkspace.add(workspaceModel.getPluginBase().getId());
        }
    
        EclipsePluginValidationOperation eclipsePluginValidationOperation = new EclipsePluginValidationOperation(
                configuration);
        eclipsePluginValidationOperation.run(null);
    
        while (eclipsePluginValidationOperation.hasErrors()) {
            Set<String> additionalIds = DependencyManager.getDependencies(addedModels.toArray(), true, null);
            if (additionalIds.isEmpty()) {
                break;
            }
            additionalIds.stream().map(PluginRegistry::findEntry).filter(Objects::nonNull).map(ModelEntry::getModel)
                    .forEach(addedModels::add);
    
            for (String id : additionalIds) {
                IPluginModelBase plugin = findPlugin(id);
                if (checkedWorkspace.contains(plugin.getPluginBase().getId())
                        && (!plugin.getPluginBase().getId().endsWith("tests"))) {
                    wBuffer.add(plugin);
                } else {
                    tBuffer.add(plugin);
                }
            }
            eclipsePluginValidationOperation.run(null);
        }
        configuration.setAttribute(IPDELauncherConstants.SELECTED_WORKSPACE_PLUGINS, wBuffer.toString());
        configuration.setAttribute(IPDELauncherConstants.SELECTED_TARGET_PLUGINS, tBuffer.toString());
    }
    
    protected IPluginModelBase findPlugin(final String id) {
        ModelEntry entry = PluginRegistry.findEntry(id);
        if (entry != null) {
            return entry.getModel();
         }
          return null;
       }
    }