以编程方式从另一个项目创建Eclipse Java项目

时间:2012-04-19 15:28:53

标签: java eclipse project

我想从另一个Java项目创建一个Java项目,使用Eclipse库中的一些脚本或Java方法,无论它是否存在。另一种方法是复制以前手动创建的项目。对此有什么办法吗?

感谢。

2 个答案:

答案 0 :(得分:1)

我相信您可以使用IProject#copy(继承自IResource.copy

答案 1 :(得分:1)

除了Alexander Pavlov的回答之外,我发现除了复制项目文件之外,还需要一些额外的工作来复制项目属性(例如引用的项目)。

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