简体   繁体   English

Eclipse 4 RCP 插件:如何以编程方式将项目导入工作区

[英]Eclipse 4 RCP Plugin: How to programmatically import projects into workspace

I am currently writing an Eclipse 4 RCP Plugin to import projects into Eclipse with the CLI.我目前正在编写一个 Eclipse 4 RCP 插件,以使用 CLI 将项目导入 Eclipse。 I found some code snippets that worked quite well to import projects before launching Eclipse.在启动 Eclipse 之前,我发现了一些代码片段可以很好地导入项目。 But when I try to use my application when Eclipse is already running, the projects do not get imported properly.但是,当我在 Eclipse 已经运行时尝试使用我的应用程序时,项目无法正确导入。

Here is the code I use so far:这是我到目前为止使用的代码:

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProjectDescription description = workspace.loadProjectDescription(new Path(projectFile.toString()));
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
if (project.isOpen() == false) {  

    project.create(description, null);
    project.open(null)

    IOverwriteQuery overwriteQuery = new IOverwriteQuery() {
        public String queryOverwrite(String file) { 
        return ALL; }
    };

    String baseDir = projectFile.getParent();
    description.setLocation(new Path(projectFile.getAbsolutePath()));
    ImportOperation importOperation = new ImportOperation(project.getFullPath(),
        new File(baseDir), FileSystemStructureProvider.INSTANCE, overwriteQuery);
    importOperation.setCreateContainerStructure(false);

    try {
        importOperation.run(null);
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    project.refreshLocal(IProject.DEPTH_INFINITE, null);
    workspace.getRoot().refreshLocal(IProject.DEPTH_INFINITE, null);
    System.out.println("Importing project  " +description.getName());

So when I execute my application while Eclipse is running, all the projects are getting created and opened, but they do not show up in the Eclipse Package Explorer and when I exit Eclipse they are all lost. So when I execute my application while Eclipse is running, all the projects are getting created and opened, but they do not show up in the Eclipse Package Explorer and when I exit Eclipse they are all lost.

What am I missing here?我在这里想念什么?

When you use 'Run as Eclipse Application' to run your program Eclipse starts a new instance of Eclipse using a separate test workspace.当您使用“作为 Eclipse 应用程序运行”运行程序时,Eclipse 使用单独的测试工作区启动 Eclipse 的新实例。 Your import is importing in to the test workspace.您的导入正在导入到测试工作区。

The Run > Run Configuration dialog will show you where the test workspace is located. Run > Run Configuration 对话框将显示测试工作区的位置。

The only way to import in to your main workspace is to install your plugin in to your main Eclipse.导入主工作区的唯一方法是将插件安装到主 Eclipse。

Note that two instances of Eclipse cannot access the same workspace safely as Eclipse is not designed to support this.请注意,Eclipse 的两个实例无法安全地访问同一个工作区,因为 Eclipse 并非旨在支持这一点。 Eclipse will normally refuse to allow this anyway but if you manage it somehow you may damage the workspace. Eclipse 通常会拒绝这样做,但如果你以某种方式管理它,你可能会损坏工作空间。

Do all your development in the separate test workspace.在单独的测试工作区中进行所有开发。 When you are sure the plug-in is working properly then you can install it in your main Eclipse.当您确定插件正常工作时,您可以将其安装在您的主 Eclipse 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM