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

可紊 提交于 2019-12-20 05:53:26

问题


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 ?


回答1:


You can't easily override the methods in EclipseApplicationLaunchConfiguration. That would require writing a new launch configuration - probably by using the org.eclipse.debug.core.launchConfigurationTypes extension point to define a new launch type.

EclipseApplicationLaunchConfiguration always uses the settings from the current entry in the 'Eclipse Application' section of the 'Run Configurations'. You can always edit the run configuration to change the dependencies or create another run configuration with different dependencies.




回答2:


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;
   }
}


来源:https://stackoverflow.com/questions/51979087/override-the-dependencies-added-during-running-a-project-as-an-eclipse-applicati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!