覆盖在将项目作为 Eclipse 应用程序运行期间添加的依赖项 [英] Override the dependencies added during running a project as an Eclipse Application

查看:10
本文介绍了覆盖在将项目作为 Eclipse 应用程序运行期间添加的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 扩展类org.eclipse.jdt.junit.launcher.JUnitLaunchShortcut
  2. 重写方法createLaunchConfiguration
  3. 调用 super.createLaunchConfiguration(element) 将返回一个 ILaunchConfigurationWorkingCopy
  4. 使用副本并设置您自己需要修改的属性
  5. 属性可以在IPDELauncherConstants
  6. 中找到
  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 默认运行工作区中的所有项目.可以通过使用创建的配置并使用自定义配置覆盖它来修改此行为.

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

这篇关于覆盖在将项目作为 Eclipse 应用程序运行期间添加的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆