将 运行 个项目期间添加的依赖项覆盖为 Eclipse 应用程序

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

我正在尝试编写自定义启动配置,同时 运行将插件项目设置为 eclipse 应用程序。我必须 运行 具有有限依赖性的插件。是否可以覆盖 org.eclipse.pde.launching.EclipseApplicationLaunchConfiguration 中的方法?如果是,那我该怎么做?

您无法轻易覆盖 EclipseApplicationLaunchConfiguration 中的方法。这将需要编写新的启动配置 - 可能通过使用 org.eclipse.debug.core.launchConfigurationTypes 扩展点来定义新的启动类型。

EclipseApplicationLaunchConfiguration 始终使用 'Run Configurations' 的 'Eclipse Application' 部分中当前条目的设置。您始终可以编辑 运行 配置以更改依赖项或创建另一个具有不同依赖项的 运行 配置。

编写自定义配置文件

  1. 延长 class org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut
  2. 覆盖方法createLaunchConfiguration
  3. 调用 super.createLaunchConfiguration(element) 将 return 一个 ILaunchConfigurationWorkingCopy
  4. 使用副本并设置自己需要修改的属性
  5. 属性可以在IPDELauncherConstants
  6. 中找到

Eclipse 默认运行工作区中找到的所有项目。可以通过使用创建的配置并使用自定义配置覆盖它来修改此行为。

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