不幸的是,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() {
}
}
您的 plugin.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 项目/* 新工作区/
将所需项目导入工作区:
eclipse -nosplash -application test.myapp.App -data NewWorkspace -import /path/to/NewWorkspace/project1 -import /path/to/NewWorkspace/project2 等...
现在,您可以安全地从 Eclipse/dropins/ 文件夹中删除 test.myapp_1.0.0.jar。
我已经上传了所有代码,包括这里导出的插件:https ://github.com/spektom/eclipse-import