不幸的是,JDT发行版没有任何支持-import参数的应用程序,就像CDT的.但你可以很容易地写一个简单的:org.eclipse.cdt.managedbuilder.core.headlessbuild
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() {
}
}
您的插件.xml应包含以下内容:
<?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>
创建插件并将其导出为“test.myapp_1.0.0.jar”。然后,您可以按如下方式使用它:
- test.myapp_1.0.0.jar复制到 Eclipse/dropins/ 文件夹中
-
将所有需要的插件复制到目标工作区目录:
cp -r projects/* NewWorkspace/
-
将所需项目导入工作区:
eclipse -nosplash -application test.myapp.App -data NewWorkspace -import /path/to/NewWorkspace/project1 -import /path/to/NewWorkspace/project2 etc...
现在,您可以安全地从 Eclipse/dropins/ 文件夹中删除 test.myapp_1.0.0.jar。
我已经上传了所有代码,包括导出的插件:https://github.com/spektom/eclipse-import