从命令行打开一个特定的 eclipse 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1087573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open a specific eclipse project from command line
提问by alex
I work with many small, but unrelated java projects. I made an Ant script that creates the .project and .classpath automatically whenever I create a new project, with the needed libraries and project name. I would like to be able to open Eclipse with that project, from the command line. Right now I do it manually, by closing the old open project in the workspace, then I do an Import and find the new project. I could not find a way to do this from either Ant or batch. I can open Eclipse, but it comes up with the last workspace/project. I don;t mind if I would have to create an individual worspace/project, but i don't know how to do that from a script. Thank you for any suggestions.
我与许多小型但不相关的 Java 项目一起工作。我制作了一个 Ant 脚本,每当我创建一个新项目时,它都会自动创建 .project 和 .classpath,并带有所需的库和项目名称。我希望能够从命令行打开带有该项目的 Eclipse。现在我手动完成,通过关闭工作区中旧的打开项目,然后我执行导入并找到新项目。我无法从 Ant 或批处理中找到执行此操作的方法。我可以打开 Eclipse,但它会出现最后一个工作区/项目。我不介意我是否必须创建一个单独的工作空间/项目,但我不知道如何从脚本中做到这一点。谢谢你的任何建议。
回答by Rich Seller
I would recommend against doing this as it is not really that much effort to import the project using the standard wizards. I'd focus on closing the inactive projects (see more below).
我建议不要这样做,因为使用标准向导导入项目并没有那么大的工作量。我会专注于关闭非活动项目(见下文)。
Edit: If you are dead set on using ant to bring the projects into the workspace, you can implement a plugin doing something like the code below.
编辑:如果您对使用 ant 将项目带入工作区一无所知,您可以实现一个插件,执行类似于以下代码的操作。
Do you close the old projects or delete them? I don't see any reason to actually delete them. If you close all projects you aren't working on (right click on them and select close project or select the project you do want and right click->close unrelated projects), they are ignored by the platform so won't impact development of the open project.
您是关闭旧项目还是删除它们?我看不出有任何理由删除它们。如果您关闭所有您不工作的项目(右键单击它们并选择关闭项目或选择您想要的项目并右键单击->关闭不相关的项目),它们将被平台忽略,因此不会影响开发打开的项目。
To hide the closed projects from the view, you can click the downwards pointing triangle in the top right corner of the Package Explorerview, select Filters...and in the Select the elements to exclude from the view:list check the Closed projectsoption.
要从视图中隐藏已关闭的项目,您可以单击Package Explorer视图右上角的向下三角形,选择Filters...并在Select the elements to exclude from the view:list 中检查Closed projects选项.
This is a plugin that will read a set of names from a file in the workspace root, delete all existing projects (without removing the contents) and create the new projects in the workspace. Use is at your own risk, no liability blah blah.
这是一个插件,它将从工作区根目录中的文件中读取一组名称,删除所有现有项目(不删除内容)并在工作区中创建新项目。使用风险自负,不承担任何责任等等。
Take the contents and put them in the relevant files and you can package an Eclipse plugin. I'd recommend using a separate Eclipse install (actually I recommend against using it at all) as it will run every time it finds the newprojects.txt in the workspace root.
把里面的内容放到相关文件中,就可以打包一个Eclipse插件了。我建议使用单独的 Eclipse 安装(实际上我完全不建议使用它),因为它每次在工作区根目录中找到 newprojects.txt 时都会运行。
The declaration in the plugin.xml implements an Eclipse extension point that is called after the workbench initializes. The earlyStartup() method of the StartupHelper is called. It creates a new Runnable that is executed asynchronously (this means the workspace loading won't block if this plugin has issues). The Runnable reads lines from the magic newprojects.txt file it expects to see in the workspace root. If it finds any contents it will delete/create the projects.
plugin.xml 中的声明实现了在工作台初始化后调用的 Eclipse 扩展点。调用 StartupHelper 的 earlyStartup() 方法。它创建了一个异步执行的新 Runnable(这意味着如果这个插件有问题,工作区加载不会阻塞)。Runnable 从它希望在工作区根目录中看到的神奇 newprojects.txt 文件中读取行。如果找到任何内容,它将删除/创建项目。
Update:The helper has been modified to allow for projects to be created outside the workspace, if you define a value in newprojects.txt it is assumed that is the absolute URI of the project. Note that it doesn't escape the string, so if you are on a windows platform, use double slashes on the path.
更新:已修改帮助程序以允许在工作区之外创建项目,如果您在 newprojects.txt 中定义值,则假定该值是项目的绝对 URI。请注意,它不会对字符串进行转义,因此如果您在 Windows 平台上,请在路径上使用双斜线。
Example contents:
示例内容:
#will be created in the workspace
project1
#will be created at c:\test\project2
project2=c:\test\project2
Good luck!
祝你好运!
/META-INF/MANIFEST.MF:
/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:
/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:
/.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:
/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:
/src/main/java/name/seller/rich/projectloader/StartupHelper .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));
}
}
回答by Paul Wagland
Another possible optionis given on this question. The essence of the answer is, if you have CDT installed, you can do:
在这个问题上给出了另一种可能的选择。答案的本质是,如果你安装了 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}
The trick here is that it can import any project, not only C projects.
这里的技巧是它可以导入任何项目,而不仅仅是 C 项目。
回答by u7867
Partial solution: Open eclipse in a specified workspace:
部分解决方案:在指定的工作空间中打开eclipse:
eclipse.exe -data c:\code\workspace-name
eclipse.exe -data c:\code\workspace-name