有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Eclipse中的java更新项目文件

我使用这个库$ 我正在尝试更改项目文件中的项目名称,使之与项目的实际名称相匹配。我从SVN repo导入了一个项目,然后用新名称重命名包含该项目的文件夹,但是如果我刷新工作区,项目文件中的名称不会更改。即使我明确告诉他:

IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription(new Path(targetProject.replace("\\", "/") + "/.project"));
description.setName(targetProject.substring(targetProject.lastIndexOf("com.")));  // here the name in description is changed
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());  // a get project from the actual description name that match with the folder name
if (!project.exists()) {
    project.create(description, monitor);
}
if (!project.isOpen()) {
    project.open(monitor);
}
project.setDescription(description, monitor);  // force the name in project to change
project.refreshLocal(IProject.DEPTH_INFINITE, monitor);  // refresh project in case that matter

// Check change
System.out.println(project.getDescription().equals(descritpion));  // false !
System.out.println(project.getDescription().getName().equals(description.getName())); // false !

好像没有什么能改变这个名字。工作区中的名称是新名称。我还试图再次关闭和打开项目,但什么也没有发生

这个代码有什么问题?任何帮助都将不胜感激

谢谢


共 (2) 个答案

  1. # 1 楼答案

    我想出了办法:

              IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription(new Path(targetProject.replace("\\", "/") + "/.project"));
              description.setName(targetProject.substring(targetProject.lastIndexOf("com.")));
              IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName());
              if (!project.exists()) {
                project.create(description, monitor);
              }
              if (!project.isOpen()) {
                project.open(monitor);
              }
              project.move(description, IProject.DEPTH_ONE, monitor);  // This change name
    

    谢谢你的帮助

  2. # 2 楼答案

    JavaDoc for IProjectDescription.setName说:

    Setting the name on a description and then setting the description on the project has no effect; the new name is ignored.

    Creating a new project with a description name which doesn't match the project handle name results in the description name being ignored; the project will be created using the name in the handle.

    所以你不能这么做