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

时间:2018-08-23 05:50:10

标签: eclipse-plugin eclipse-rcp rcp eclipse-pde pde

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

2 个答案:

答案 0 :(得分:0)

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

EclipseApplicationLaunchConfiguration始终使用“运行配置”的“ Eclipse应用程序”部分中当前条目中的设置。您始终可以编辑运行配置以更改依赖关系,或创建具有不同依赖关系的另一个运行配置。

答案 1 :(得分:0)

编写自定义配置文件

  1. 扩展类org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut
  2. 覆盖方法createLaunchConfiguration
  3. 调用super.createLaunchConfiguration(element)将返回一个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;
   }
}