英文:
Update project file in Eclipse
问题
我使用这个库 <pre>org.eclipse.core.resources</pre>
我正在尝试更改项目文件中的项目名称,使其与项目的实际名称相匹配。我从SVN仓库导入了一个项目,然后将包含该项目的文件夹重命名为新名称,但如果我刷新工作区,项目文件中的名称不会更改。即使我明确告诉它:
IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription(new Path(targetProject.replace("\\", "/") + "/.project"));
description.setName(targetProject.substring(targetProject.lastIndexOf("com."))); // 这里description中的名称已更改
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName()); // 根据与文件夹名称匹配的实际描述名称获取项目
if (!project.exists()) {
project.create(description, monitor);
}
if (!project.isOpen()) {
project.open(monitor);
}
project.setDescription(description, monitor); // 强制更改项目中的名称
project.refreshLocal(IProject.DEPTH_INFINITE, monitor); // 刷新项目以防有影响的情况发生
// 检查更改
System.out.println(project.getDescription().equals(description)); // false!
System.out.println(project.getDescription().getName().equals(description.getName())); // false!
就好像没有任何东西能够改变这个名称。工作区中的名称是新名称。我还尝试了关闭并重新打开项目,但没有任何变化。
这段代码有什么问题?任何帮助都将不胜感激。
谢谢。
英文:
I use this lib <pre>org.eclipse.core.resources</pre>
I'm trying to change name of project in the project file to match with the actual name of project progammaticaly. I imported a project from a SVN repo and then rename the folder that content this project with a new name, but if I refresh the workspace, the name in the project file doesn't change. Even if I tell him specifically :
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 !
It's like nothing can change this name. The name in workspace is the new name. I also try to close and open the project again but nothing happen.
What is wrong in this code ? Any help will be appreciate.
Thanks.
答案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
谢谢你的帮助。
英文:
I figured out how to do this :
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
Thanks for your help.
答案2
得分: 0
IProjectDescription.setName
的Java文档说明:
> 在描述中设置名称,然后在项目上设置描述无效;新名称将被忽略。
>
> 使用与项目句柄名称不匹配的描述名称创建新项目将导致描述名称被忽略;项目将使用句柄中的名称进行创建。
因此,您不能这样做。
英文:
The JavaDoc for IProjectDescription.setName
says:
> 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.
So you can't do this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论