以编程方式生成 Eclipse 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/251807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically generate an Eclipse project
提问by Kris Pruden
I use eclipse to work on an application which was originally created independently of eclipse. As such, the application's directory structure is decidedly not eclipse-friendly.
我使用 eclipse 来处理最初独立于 eclipse 创建的应用程序。因此,应用程序的目录结构显然不是 eclipse 友好的。
I want to programmatically generate a project for the application. The .project
and .classpath
files are easy enough to figure out, and I've learned that projects are stored in the workspace under <workspace>/.metadata/.plugins/org.eclipse.core.resources/.projects
我想以编程方式为应用程序生成一个项目。在.project
和.classpath
文件是很容易的弄清楚,我已经了解到,项目存储在工作区下<workspace>/.metadata/.plugins/org.eclipse.core.resources/.projects
Unfortunately, some of the files under here (particularly .location
) seem to be encoded in some kind of binary format. On a hunch I tried to deserialize it using ObjectInputStream
- no dice. So it doesn't appear to be a serialized java object.
不幸的是,这里的一些文件(特别是.location
)似乎是以某种二进制格式编码的。凭直觉,我尝试使用ObjectInputStream
- 没有骰子反序列化它。所以它似乎不是一个序列化的 java 对象。
My question is: is there a way to generate these files automatically?
我的问题是:有没有办法自动生成这些文件?
For the curious, the error I get trying to deserialize the .location
file is the following:
出于好奇,我在尝试反序列化.location
文件时遇到的错误如下:
java.io.StreamCorruptedException: java.io.StreamCorruptedException: invalid stream header: 40B18B81
java.io.StreamCorruptedException: java.io.StreamCorruptedException: invalid stream header: 40B18B81
Update:My goal here is to be able to replace the New Java Project wizard with a command-line script or program. The reason is the application in question is actually a very large J2EE/weblogic application, which I like to break down into a largish (nearly 20) collection of subprojects. Complicating matters, we use clearcase for SCM and create a new branch for every release. This means I need to recreate these projects for every development view (branch) I create. This happens often enough to automate.
更新:我的目标是能够用命令行脚本或程序替换 New Java Project 向导。原因是所讨论的应用程序实际上是一个非常大的 J2EE/weblogic 应用程序,我喜欢将其分解为一个较大的(近 20 个)子项目集合。更复杂的是,我们为 SCM 使用 clearcase 并为每个版本创建一个新分支。这意味着我需要为我创建的每个开发视图(分支)重新创建这些项目。这种情况经常发生,足以自动化。
回答by James Van Huis
You should be able to accomplish this by writing a small Eclipse plugin. You could even extend it out to being a "headless" RCP app, and pass in the command line arguments you need.
您应该能够通过编写一个小的 Eclipse 插件来实现这一点。您甚至可以将其扩展为“无头”RCP 应用程序,并传入您需要的命令行参数。
The barebones code to create a project is:
创建项目的准系统代码是:
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("DesiredProjectName");
project.create(progressMonitor);
project.open(progressMonitor);
Just take a look at the eclipse code for the Import Project wizard to give you a better idea of where to go with it.
只需查看导入项目向导的 eclipse 代码,您就可以更好地了解如何使用它。