简体   繁体   English

以编程方式生成 Eclipse 项目

[英]Programmatically generate an Eclipse project

I use eclipse to work on an application which was originally created independently of eclipse.我使用 eclipse 来处理最初独立于 eclipse 创建的应用程序。 As such, the application's directory structure is decidedly not eclipse-friendly.因此,应用程序的目录结构显然不是 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下的<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.不幸的是,这里的一些文件(特别是.location )似乎是以某种二进制格式编码的。 On a hunch I tried to deserialize it using ObjectInputStream - no dice.凭直觉,我尝试使用ObjectInputStream反序列化它 - 没有骰子。 So it doesn't appear to be a serialized java object.所以它似乎不是一个序列化的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

Update: My goal here is to be able to replace the New Java Project wizard with a command-line script or program.更新:我的目标是能够用命令行脚本或程序替换 New Java Project 向导。 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.原因是所讨论的应用程序实际上是一个非常大的 J2EE/weblogic 应用程序,我喜欢将其分解为一个较大的(近 20 个)子项目集合。 Complicating matters, we use clearcase for SCM and create a new branch for every release.更复杂的是,我们为 SCM 使用 clearcase 并为每个版本创建一个新分支。 This means I need to recreate these projects for every development view (branch) I create.这意味着我需要为我创建的每个开发视图(分支)重新创建这些项目。 This happens often enough to automate.这种情况经常发生,足以自动化。

You should be able to accomplish this by writing a small Eclipse plugin.您应该能够通过编写一个小的 Eclipse 插件来实现这一点。 You could even extend it out to being a "headless" RCP app, and pass in the command line arguments you need.您甚至可以将其扩展为“无头”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 代码即可让您更好地了解如何使用它。

Use AntEclipse使用AntEclipse

It can create eclipse projects from ant.它可以从ant创建eclipse项目。

To create java project you can use JavaCore from org.eclipse.jdt.core.JavaCore .要创建 Java 项目,您可以使用org.eclipse.jdt.core.JavaCore JavaCore。 As a sourceProject you can use generic project item, which has been suggested by @James Van Huis作为sourceProject您可以使用@James Van Huis 建议的通用项目项

IJavaProject javaSourceProject = JavaCore.create(sourceProject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM