17

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.

Kris Pruden
  • 3,280
  • 4
  • 25
  • 30

3 Answers3

19

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.

James Van Huis
  • 5,481
  • 1
  • 26
  • 25
  • This way of creating project works on eclipse plugin project. Is there any way to create a java project from standalone java project? means creating a project from main class simply by running it. Thanks in advance. – Abhishek Tiwari Aug 06 '16 at 14:35
  • This answer would be even more helpful if it stated where the code for the Import Project wizard can be found. Just the name of the containing package and/or the name of the implementing class might be enough to find it in the eclipse git repositories. – M_M Oct 18 '19 at 12:21
2

Use AntEclipse

It can create eclipse projects from ant.

Tim Williscroft
  • 3,705
  • 24
  • 37
0

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);