2012-04-19 97 views
1

我想從另一個Java項目創建一個Java項目,使用Eclipse庫中的一些腳本或Java方法(無論它是否存在)。此替代可以複製以前手動創建的項目。有沒有辦法呢?從另一個項目創建一個Eclipse Java項目,編程式

謝謝。

+0

您需要以編程方式或手動執行此操作嗎? – Th0rndike 2012-04-19 15:32:28

+1

類似的問題http://stackoverflow.com/q/251807/858356 – cyberbemon 2012-04-19 15:34:03

+0

我想以編程方式做到這一點。 – 2012-04-20 10:34:53

回答

1

添加到亞歷山大·巴甫洛夫的答案,我發現一些額外的工作需要,除了複製的項目屬性(如引用的項目)只是複製項目文件。

public static IProject copyProject(String projectName) throws CoreException { 
    IProgressMonitor m = new NullProgressMonitor(); 
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 
    IProject project = workspaceRoot.getProject(projectName); 
    IProjectDescription projectDescription = project.getDescription(); 
    String cloneName = projectName + "_copy"; 
    // create clone project in workspace 
    IProjectDescription cloneDescription = workspaceRoot.getWorkspace().newProjectDescription(cloneName); 
    // copy project files 
    project.copy(cloneDescription, true, m); 
    IProject clone = workspaceRoot.getProject(cloneName); 
    // copy the project properties 
    cloneDescription.setNatureIds(projectDescription.getNatureIds()); 
    cloneDescription.setReferencedProjects(projectDescription.getReferencedProjects()); 
    cloneDescription.setDynamicReferences(projectDescription.getDynamicReferences()); 
    cloneDescription.setBuildSpec(projectDescription.getBuildSpec()); 
    cloneDescription.setReferencedProjects(projectDescription.getReferencedProjects()); 
    clone.setDescription(cloneDescription, null); 
    return clone; 
}