[英]How to create packages (folders) in an Eclipse project via plugin
I try to develop a small plugin for Eclipse to create several Java files in several folders (packages) as a starting point for a new module of a larger software. 我尝试为Eclipse开发一个小插件,在几个文件夹(包)中创建几个Java文件,作为大型软件新模块的起点。
I've tried to use an IFile
object like this: 我试过像这样使用
IFile
对象:
final IFile file = container.getFile(new Path(myFileName));
...
file.create(stream, true, monitor);
That works as long as all folders on the path to the file exists. 只要文件路径上的所有文件夹都存在,该方法就可以正常工作。 But it does not create any missing folders (new packages) but throws a "resource not exists" exception.
但它不会创建任何丢失的文件夹(新包),但会抛出“资源不存在”异常。
I could not find any way to do this by IResource
or IWorkspace
objects. 我无法通过
IResource
或IWorkspace
对象找到任何方法。
Personally, I use a small method which recursively creates all of the folders, something like: 就个人而言,我使用一个小方法递归创建所有文件夹,如:
IFile file = project.getFile(newPath);
prepare((IFolder) file.getParent());
and then the method 然后是方法
public void prepare(IFolder folder) {
if (!folder.exists()) {
prepare((IFolder) folder.getParent())
folder.create(false, false, null);
}
}
This works well for me. 这对我很有用。
I know this does not answer your question, but may I suggest you take a look at Maven Archetypes ? 我知道这不能回答你的问题,但我建议你看看Maven Archetypes吗? This way, you could create project templates with the desired directory structure and boilerplate files, in a configurable and non Eclipse-dependant way.
这样,您可以使用所需的目录结构和样板文件,以可配置且不依赖于Eclipse的方式创建项目模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.