Eclipse - 如何处理自定义项目创建
Eclipse - how to handle custom projects creation
我想在我自己的 Eclipse 特性中开发能力来创建我自己的 DSL 语言的项目 - 换句话说,我想要一个向导,这样我就可以单击 New > My Custom Project
,然后在向导中填写所有必填字段页面,然后在执行 Finish
之后,我想创建基本的文件夹层次结构和模板文件,并添加我的 DSL 性质。我知道如何创建 plugins/features,和向导一样好。我可以在 "hard-code" 中创建所有内容,我的意思是创建文件夹和文件,但也许有一些内置解决方案可以促进它?我找到了 IProject
接口和它的东西,这是一个很好的线索吗?
假设您的插件包含一个包含所有模板文件的子文件夹 template
,您可以将文件结构递归复制到新创建的项目中:
Bundle bundle = MyPlugin.getDefault().getBundle();
String templateFolderName = "template";
for (Enumeration entries = bundle.findEntries(templateFolderName, "*", true);
entries.hasMoreElements();) {
URL url = (URL) entries.nextElement();
String path = url.getPath();
if (!path.startsWith("/" + templateFolderName + "/"))
throw new InvocationTargetException(
new Throwable("Unknown template file: " + path));
// create folder or file (overwrite if file already exists)
String targetPath = path.substring(("/" + templateFolderName).length());
if (path.endsWith("/")) {
IFolder folder = project.getFolder(targetPath);
if (! folder.exists()) {
folder.create(false, true, null);
}
} else {
InputStream in = url.openStream();
IFile file = project.getFile(targetPath);
if (file.exists()) {
file.delete(true, null);
}
file.create(in, true, null);
}
}
(参见 source of the code snippet above)
参见示例 this implementation of a customized New Project wizard dialog。
我在 Eclipse FAQ 站点上发现了 org.eclipse.core.resources.* 库的有趣实现,它似乎可以工作。
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("MyProject");
IFolder folder = project.getFolder("Folder1");
IFile file = folder.getFile("hello.txt");
//at this point, no resources have been created
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!folder.exists())
folder.create(IResource.NONE, true, null);
if (!file.exists()) {
byte[] bytes = "File contents".getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
它非常简单并且很好地完成了它的工作,可以轻松初始化项目的内容
我想在我自己的 Eclipse 特性中开发能力来创建我自己的 DSL 语言的项目 - 换句话说,我想要一个向导,这样我就可以单击 New > My Custom Project
,然后在向导中填写所有必填字段页面,然后在执行 Finish
之后,我想创建基本的文件夹层次结构和模板文件,并添加我的 DSL 性质。我知道如何创建 plugins/features,和向导一样好。我可以在 "hard-code" 中创建所有内容,我的意思是创建文件夹和文件,但也许有一些内置解决方案可以促进它?我找到了 IProject
接口和它的东西,这是一个很好的线索吗?
假设您的插件包含一个包含所有模板文件的子文件夹 template
,您可以将文件结构递归复制到新创建的项目中:
Bundle bundle = MyPlugin.getDefault().getBundle();
String templateFolderName = "template";
for (Enumeration entries = bundle.findEntries(templateFolderName, "*", true);
entries.hasMoreElements();) {
URL url = (URL) entries.nextElement();
String path = url.getPath();
if (!path.startsWith("/" + templateFolderName + "/"))
throw new InvocationTargetException(
new Throwable("Unknown template file: " + path));
// create folder or file (overwrite if file already exists)
String targetPath = path.substring(("/" + templateFolderName).length());
if (path.endsWith("/")) {
IFolder folder = project.getFolder(targetPath);
if (! folder.exists()) {
folder.create(false, true, null);
}
} else {
InputStream in = url.openStream();
IFile file = project.getFile(targetPath);
if (file.exists()) {
file.delete(true, null);
}
file.create(in, true, null);
}
}
(参见 source of the code snippet above)
参见示例 this implementation of a customized New Project wizard dialog。
我在 Eclipse FAQ 站点上发现了 org.eclipse.core.resources.* 库的有趣实现,它似乎可以工作。
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("MyProject");
IFolder folder = project.getFolder("Folder1");
IFile file = folder.getFile("hello.txt");
//at this point, no resources have been created
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!folder.exists())
folder.create(IResource.NONE, true, null);
if (!file.exists()) {
byte[] bytes = "File contents".getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
它非常简单并且很好地完成了它的工作,可以轻松初始化项目的内容