从命令行打开特定的eclipse项目

从命令行打开特定的eclipse项目,eclipse,Eclipse,我处理许多小型但不相关的java项目。我制作了一个Ant脚本,每当我创建一个新项目时,它会自动创建.project和.classpath,并带有所需的库和项目名称。我希望能够从命令行使用该项目打开Eclipse。现在我手动完成,关闭工作区中打开的旧项目,然后导入并找到新项目。我无法从Ant或batch中找到这样做的方法。我可以打开Eclipse,但它提供了最后一个工作区/项目。我不知道;我不介意我是否必须创建一个单独的工作空间/项目,但我不知道如何从脚本中创建。感谢您的建议。部分解决方案:在指定

我处理许多小型但不相关的java项目。我制作了一个Ant脚本,每当我创建一个新项目时,它会自动创建.project和.classpath,并带有所需的库和项目名称。我希望能够从命令行使用该项目打开Eclipse。现在我手动完成,关闭工作区中打开的旧项目,然后导入并找到新项目。我无法从Ant或batch中找到这样做的方法。我可以打开Eclipse,但它提供了最后一个工作区/项目。我不知道;我不介意我是否必须创建一个单独的工作空间/项目,但我不知道如何从脚本中创建。感谢您的建议。

部分解决方案:在指定的工作区中打开eclipse:


eclipse.exe-data c:\code\workspace name

我建议不要这样做,因为使用标准向导导入项目实际上不需要太多工作。我会专注于关闭不活跃的项目(见下文)

编辑:如果你执意使用ant将项目带入工作区,你可以实现一个插件,做如下代码

您是关闭旧项目还是删除它们?我看不出有任何理由真正删除它们。如果您关闭所有不在处理的项目(右键单击它们并选择close project,或选择您想要的项目并右键单击->close unrelated project),平台将忽略它们,因此不会影响打开项目的开发

要从视图中隐藏关闭的项目,您可以单击包资源管理器视图右上角的向下三角形,选择过滤器…,然后在中选择要从视图中排除的元素:列表选中关闭的项目选项


这是一个插件,它将从工作区根目录中的文件中读取一组名称,删除所有现有项目(不删除内容),并在工作区中创建新项目。使用风险自负,不承担任何责任

获取内容并将其放入相关文件中,您可以打包一个Eclipse插件。我建议使用单独的Eclipse安装(实际上我建议不要使用它),因为每次在工作区根目录中找到newprojects.txt时,它都会运行

plugin.xml中的声明实现了在工作台初始化后调用的Eclipse扩展点。调用StartupHelper的earlyStartup()方法。它创建一个异步执行的新Runnable(这意味着如果这个插件有问题,工作区加载将不会阻塞)。Runnable从它希望在工作区根目录中看到的magic newprojects.txt文件中读取行。如果找到任何内容,它将删除/创建项目

更新: 已修改帮助器以允许在工作区之外创建项目,如果在newprojects.txt中定义值,则假定该值是项目的绝对URI。请注意,它不会转义字符串,因此如果您在windows平台上,请在路径上使用双斜杠

示例内容:

#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>
/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));
    }
}
另一个是关于。答案的实质是,如果您安装了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}

这里的诀窍是,它可以导入任何项目,而不仅仅是C项目。

如前所述,我已经可以这样做了,但没有多大帮助。我仍然需要关闭上一个项目并通过导入来打开新项目。首先感谢您的回答。这是一个有趣的拍摄,我会看看,但它可能看起来有点偏僻。关于我的设置还有几句话:我们通过预处理从“主”代码库生成java项目(.java,参考资料)。然后我们调整并编译成JAR。有时我们需要在Eclipse中进行调试,这时我们手动创建一个项目并导入源代码、资源和库。我现在使这个过程自动化了,我想通过使用新创建的项目打开Eclipse来结束代码生成。旧的项目是一次性的(关闭/删除)。我不是死心塌地地地使用Ant,任何东西都可以使用,包括batch(Win platform)。这是一个小技巧,但你可以定义一个插件,在Eclipse启动时检查一个众所周知的文件中的“新”项目。通过实现IStartup接口,将调用earlyStartup()方法,您可以如上所述创建项目。如果我有时间的话,我会安排一个更详细的任务!我以为会有链接什么的,但你全力以赴了。非常感谢。我将尝试将其集成到构建过程中,但由于我的脚本不完整,这将需要几天的时间。我有两个插件,它们之间可以完成99%的工作(一个做早期启动,另一个创建项目),所以只需几分钟就可以编写和测试,我的主要问题是读取属性文件的方式很优雅,请参阅
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);
    }

}
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));
    }
}
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}