简体   繁体   English

如何通过插件在Eclipse项目中创建包(文件夹)

[英]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. 我无法通过IResourceIWorkspace对象找到任何方法。

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.

相关问题 Eclipse插件-刷新Eclipse项目中的链接文件夹 - Eclipse plugin - refresh linked folders in eclipse project 以编程方式创建插件Eclipse项目 - Create Plugin Eclipse project programmatically 仅从文件夹创建eclipse项目 - To create an eclipse project from just the folders 文件夹在 Eclipse 中显示为包 - Folders showing up as packages in Eclipse 在开发Eclipse插件以在另一个项目中创建项目时,如何获取当前项目位置。 - How to get current project location when I developing a Eclipse plugin to create a project within another project. 如何在导入项目时将Eclipse识别为“代码文件夹”? - How to make Eclipse recognize folders as “code folders” when a project is imported? 如何为eclipse插件项目创建一个jar文件,该文件应该能够在没有Eclipse软件的情况下下载并运行? - How to create a jar file for the eclipse plugin project that should be able to download and run without eclipse software? 如何使用Eclipse Indigo使用m2eclipse插件创建struts 2项目? - How to create a struts 2 project using m2eclipse plugin using Eclipse Indigo? 无法在 Eclipse 插件“Google Cloud Tools for Eclipse”中创建项目 - Failed to create project in Eclipse plugin "Google Cloud Tools for Eclipse" 创建一个向导/插件,以在eclipse插件中生成一个新项目 - create a wizard/plugin which generates a new project in eclipse plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM