Automate import of Java (Android) projects into Eclipse workspace through commandline
Unfortunately, JDT distribution doesn't have any application that would support -import argument, like CDT's org.eclipse.cdt.managedbuilder.core.headlessbuild
. But you can easily write a simple one:
package test.myapp;import java.util.LinkedList;import java.util.List;import org.eclipse.core.resources.IProject;import org.eclipse.core.resources.IProjectDescription;import org.eclipse.core.resources.IncrementalProjectBuilder;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.core.runtime.NullProgressMonitor;import org.eclipse.core.runtime.Path;import org.eclipse.equinox.app.IApplication;import org.eclipse.equinox.app.IApplicationContext;public class Application implements IApplication { public Object start(IApplicationContext context) throws Exception { String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS); boolean build = false; // Determine projects to import List<String> projects = new LinkedList<String>(); for (int i = 0; i < args.length; ++i) { if ("-import".equals(args[i]) && i + 1 < args.length) { projects.add(args[++i]); } else if ("-build".equals(args[i])) { build = true; } } if (projects.size() == 0) { System.out.println("No projects to import!"); } else { for (String projectPath : projects) { System.out.println("Importing project from: " + projectPath); // Import project description: IProjectDescription description = ResourcesPlugin.getWorkspace().loadProjectDescription( new Path(projectPath).append(".project")); IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(description.getName()); project.create(description, null); project.open(null); } // Build all projects after importing if (build) { System.out.println("Re-building workspace"); ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor()); ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); } } return null; } public void stop() { }}
Your plugin.xml should contain something like:
<?xml version="1.0" encoding="UTF-8"?><?eclipse version="3.4"?><plugin> <extension id="App" point="org.eclipse.core.runtime.applications"> <application cardinality="singleton-global" thread="main" visible="true"> <run class="test.myapp.Application"> </run> </application> </extension></plugin>
Create, and export your plug-in as "test.myapp_1.0.0.jar". Then you can use it as follows:
- Copy test.myapp_1.0.0.jar to your Eclipse/dropins/ folder
Copy all needed plug-ins to the target workspace directory:
cp -r projects/* NewWorkspace/
Import needed projects into the workspace:
eclipse -nosplash -application test.myapp.App -data NewWorkspace -import /path/to/NewWorkspace/project1 -import /path/to/NewWorkspace/project2 etc...
Now, you can safely remove test.myapp_1.0.0.jar from the Eclipse/dropins/ folder.
I've uploaded all the code, including the exported plug-in here: https://github.com/spektom/eclipse-import