Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate import of Java (Android) projects into Eclipse workspace through commandline

I'm trying to automate importing projects to an Eclipse workspace via commandline (using a bash script). I have seen many posts suggesting using the CDT headless build even for non-C/C++ projects, but I want to avoid having to download CDT as my projects are all Java/Android projects and I want to be able to automate this for many people without having to make them all download CDT. I have tried the following with the JDT headless build with no avail:

eclipse -nosplash \
    -application org.eclipse.jdt.apt.core.aptBuild \
    -data [absolute_path_to_desired_workspace] \
    -import [absolute_path_to_project_directories]

Output shows "Building workspace" and then "logout," but opening a session of Eclipse in the workspace shows nothing in the Package explorer.

Looking at the ./metadata/.log file in the workspace doesn't seem to show any errors with the import.

Is it not possible to automate the import of existing Java Eclipse projects into Eclipse without using the CDT headless build?

like image 658
socoho Avatar asked Jul 02 '12 22:07

socoho


1 Answers

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:

  1. Copy test.myapp_1.0.0.jar to your Eclipse/dropins/ folder
  2. Copy all needed plug-ins to the target workspace directory:

    cp -r projects/* NewWorkspace/

  3. 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...

  4. 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

like image 107
Michael Spector Avatar answered Sep 28 '22 16:09

Michael Spector