简体   繁体   中英

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. I found some code snippets that worked quite well to import projects before launching Eclipse. But when I try to use my application when Eclipse is already running, the projects do not get imported properly.

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.

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. Your import is importing in to the test workspace.

The Run > Run Configuration dialog will show you where the test workspace is located.

The only way to import in to your main workspace is to install your plugin in to your main Eclipse.

Note that two instances of Eclipse cannot access the same workspace safely as Eclipse is not designed to support this. Eclipse will normally refuse to allow this anyway but if you manage it somehow you may damage the workspace.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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