2009-12-28 55 views

回答

2

你必须here the same idea通过Liran Orevi但也有一些更多的细节和代码示例表示:

/** 
* Imports the given path into the workspace as a project. Returns true if the 
* operation succeeded, false if it failed to import due to an overlap. 
* 
* @param projectPath 
* @return 
* @throws CoreException if operation fails catastrophically 
*/ 
private boolean importExisitingProject(IPath projectPath) throws CoreException { 
    // Load the project description file 
    final IProjectDescription description = workspace.loadProjectDescription(
    projectPath.append(IPath.SEPARATOR + IProjectDescription.DESCRIPTION_FILE_NAME)); 
    final IProject project = workspace.getRoot().getProject(description.getName()); 

    // Only import the project if it doesn't appear to already exist. If it looks like it 
    // exists, tell the user about it. 
    if (project.exists()) { 
     System.err.println(SKTBuildPlugin.getFormattedMessage(
     "Build.commandLine.projectExists", //$NON-NLS-1$ 
     project.getName())); 
     return false; 
    } 
    IWorkspaceRunnable runnable = new IWorkspaceRunnable() { 
     public void run(IProgressMonitor monitor) throws CoreException { 
      project.create(description, monitor); 
      project.open(IResource.NONE, monitor); 
     } 
    }; 
    workspace.run(runnable, 
    workspace.getRuleFactory().modifyRule(workspace.getRoot()), 
    IResource.NONE, null); 
    return true; 
} 

this thread,你也可以导入压缩的项目,其中一些代码灵感来源于大多数从org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.java中被刮掉的代码。

+0

好的,所以代码有点复杂(我也在做晚餐)。我可以简单地调用现有的ImportWizard并保存自己写下来吗? – 2009-12-28 08:48:59

+0

@Mechko:我不确定,因为我没有直接测试过这个代码,但是希望这可以给你足够的材料来试用它。 – VonC 2009-12-28 10:01:31

+0

这似乎是正确的,但我无法在有限的时间内使用它,因为我不知道如何获取工作空间实例变量。我修补了一个bash脚本,它将完成我现在需要的工作(我需要运行一个java项目,这是我的java项目生成的。)但是我最终需要这个,所以非常感谢 – 2009-12-28 10:07:46