Kris Pruden
Kris Pruden

Reputation: 3308

Programmatically generate an Eclipse project

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.

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

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.

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:

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.

Upvotes: 17

Views: 15529

Answers (3)

mikolayek
mikolayek

Reputation: 1

To create java project you can use JavaCore from org.eclipse.jdt.core.JavaCore. As a sourceProject you can use generic project item, which has been suggested by @James Van Huis

IJavaProject javaSourceProject = JavaCore.create(sourceProject);

Upvotes: 0

Tim Williscroft
Tim Williscroft

Reputation: 3756

Use AntEclipse

It can create eclipse projects from ant.

Upvotes: 2

James Van Huis
James Van Huis

Reputation: 5571

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.

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.

Upvotes: 20

Related Questions