我想从另一个 Java 项目创建一个 Java 项目,使用 Eclipse 库中的一些脚本或 Java 方法,无论它是否存在。另一种方法是复制以前手动创建的项目。有什么办法吗?
谢谢。
我相信你可以利用IProject#copy
(继承自IResource.copy)
添加到 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;
}