我与许多小型但不相关的Java项目一起工作。我创建了一个Ant脚本,可以在创建新项目时自动创建.project和.classpath文件,并加入所需的库和项目名称。 我希望能够从命令行打开Eclipse并使用该项目。目前,我手动关闭工作区中旧的打开项目,然后进行导入并查找新项目。我无法找到从Ant或批处理文件执行此操作的方法。我可以打开Eclipse,但它会显示上一个工作区/项目。如果必须从脚本中创建单独的工作区/项目,我也不介意,但我不知道如何做到这一点。感谢任何建议。
#will be created in the workspace
project1
#will be created at c:\test\project2
project2=c:\\test\project2
祝你好运!
/META-INF/MANIFEST.MF:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Project fettling Plug-in
Bundle-SymbolicName: name.seller.rich;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: name.seller.rich.Activator
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui.workbench;bundle-version="3.4.1",
org.eclipse.swt;bundle-version="3.4.1",
org.eclipse.core.resources;bundle-version="3.4.1"
Bundle-ActivationPolicy: lazy
/plugin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension
point="org.eclipse.ui.startup">
<startup class="name.seller.rich.projectloader.StartupHelper"/>
</extension>
</plugin>
/.project:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>name.seller.rich.projectloader</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
/.classpath:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
/src/main/java/name/seller/rich/Activator.java:
package name.seller.rich;
import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends Plugin {
// The plug-in ID
public static final String PLUGIN_ID = "name.seller.rich";
// The shared instance
private static Activator plugin;
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
/**
* The constructor
*/
public Activator() {
}
@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
@Override
public void stop(final BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
}
/src/main/java/name/seller/rich/projectloader/StartupHelper.java:
这是一个Java文件的路径。package name.seller.rich.projectloader;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import name.seller.rich.Activator;
import org.eclipse.core.internal.resources.ProjectDescription;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
public class StartupHelper implements IStartup {
private static final class DirtyHookRunnable implements Runnable {
private IWorkspaceRoot workspaceRoot;
private DirtyHookRunnable(final IWorkspaceRoot workspaceRoot) {
this.workspaceRoot = workspaceRoot;
}
public void run() {
try {
IPath workspaceLocation = this.workspaceRoot.getLocation();
File startupFile = new File(workspaceLocation.toOSString(),
"newprojects.txt");
IProgressMonitor monitor = new NullProgressMonitor();
Properties properties = new Properties();
if (startupFile.exists()) {
properties.load(new FileInputStream(startupFile));
}
if (properties.size() > 0) {
// delete existing projects
IProject[] projects = this.workspaceRoot.getProjects();
for (IProject project : projects) {
// don't delete the content
project.delete(false, true, monitor);
}
// create new projects
for (Map.Entry entry : properties.entrySet()) {
IProject project = this.workspaceRoot
.getProject((String) entry.getKey());
// insert into loop
ProjectDescription projectDescription = new ProjectDescription();
projectDescription.setName((String) entry.getKey());
String location = (String) entry.getValue();
// value will be empty String if no "=" on the line
// in that case it will be created in the workspace
// WARNING, currently windows paths must be escaped,
// e.g. c:\\test\\myproject
if (location.length() > 0) {
IPath locationPath = new Path(location);
projectDescription.setLocation(locationPath);
}
project.create(projectDescription, monitor);
// project.create(monitor);
project.open(monitor);
}
}
} catch (Exception e) {
IStatus status = new Status(IStatus.INFO, Activator.PLUGIN_ID,
0, "unable to load new projects", null);
Activator.getDefault().getLog().log(status);
}
}
}
public StartupHelper() {
super();
}
public final void earlyStartup() {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
workbench.getDisplay().asyncExec(new DirtyHookRunnable(workspaceRoot));
}
}
这个问题 的答案 给出了另一个可能的选项。回答的要点是,如果你已经安装了CDT,可以执行以下操作:
eclipse -nosplash
-application org.eclipse.cdt.managedbuilder.core.headlessbuild
-import {[uri:/]/path/to/project}
-importAll {[uri:/]/path/to/projectTreeURI} Import all projects under URI
-build {project_name | all}
-cleanBuild {projec_name | all}
org.eclipse.ant.core.antRunner
。 - milahu完整的选项列表是有帮助的,正如@Paul Wagland所指出的那样,但直到我使用了这个命令,其中包括-data
选项,我才得以使其工作:
user@devmachine:~/dev$ eclipse -nosplash -data /home/$USER/dev -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import /home/$USER/dev/src_dir