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

8
我与许多小型但不相关的Java项目一起工作。我创建了一个Ant脚本,可以在创建新项目时自动创建.project和.classpath文件,并加入所需的库和项目名称。 我希望能够从命令行打开Eclipse并使用该项目。目前,我手动关闭工作区中旧的打开项目,然后进行导入并查找新项目。我无法找到从Ant或批处理文件执行此操作的方法。我可以打开Eclipse,但它会显示上一个工作区/项目。如果必须从脚本中创建单独的工作区/项目,我也不介意,但我不知道如何做到这一点。感谢任何建议。
4个回答

7
我建议不要这样做,因为使用标准向导程序导入项目并不需要太多的努力。我建议专注于关闭不活动的项目(请参见下文)。
编辑:如果您一定要使用Ant将项目带入工作区,您可以实现一个插件来执行以下代码。
您是关闭旧项目还是删除它们?我不认为有必要真正删除它们。如果您关闭所有不工作的项目(右键单击它们并选择关闭项目或选择您想要的项目并右键单击->关闭不相关项目),则平台将忽略它们,因此不会影响打开项目的开发。
要从视图中隐藏已关闭的项目,可以单击“Package Explorer”视图右上角的向下箭头,选择“Filters...”,在“选择要从视图中排除的元素:”列表中选中“Closed projects”选项。
这是一个插件,它将从工作区根目录中的文件中读取一组名称,删除所有现有项目(而不删除内容)并在工作区中创建新项目。风险自负,没有责任等等。
将内容放入相关文件中,即可打包Eclipse插件。我建议使用单独的Eclipse安装(实际上我建议不要使用它),因为每次在工作区根目录中找到newprojects.txt时都会运行它。 plugin.xml中的声明实现了一个称为工作台初始化后调用的Eclipse扩展点。 StartupHelper的earlyStartup()方法被调用。它创建一个新的Runnable,异步执行(这意味着如果此插件有问题,则不会阻止工作区加载)。 Runnable从魔术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>

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

我并不一定要使用Ant,任何工具都可以,包括批处理(Win平台)。 - alex
这有点像是一个hack,但你可以定义一个插件,在Eclipse启动时检查一个众所周知的文件以获取“新”项目。通过实现IStartup接口,将调用earlyStartup()方法,然后你可以像上面那样创建项目。如果我有时间,我会提供更详细的回复。 - Rich Seller
哇!我本来以为你只会给个链接什么的,但你真的做得很全面。谢谢你。我会尝试将其集成到构建过程中,但需要几天时间,因为我的脚本还不完整。完成后,我会发表我的评论。 - alex
我有两个插件,它们之间完成了99%的工作(一个用于早期启动,另一个用于创建项目),因此只需要几分钟来编写和测试,我的主要问题是一种优雅的方式来读取属性文件,请参见https://dev59.com/K0jSa4cB1Zd3GeqPEVA3 - Rich Seller
+1,非常棒。这几乎就是我在寻找的东西。 - Paul Wagland
显示剩余4条评论

6

这个问题 的答案 给出了另一个可能的选项。回答的要点是,如果你已经安装了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项目,存在org.eclipse.ant.core.antRunner - milahu
@MilaNautikus - 我没有看到那个应用程序导入项目的选项? - Paul Wagland
1
不好意思,我需要构建一个Java项目。最终,我做了以下几步:启动Eclipse GUI,导出“build.xml”文件,运行“ant”。 - milahu

1

完整的选项列表是有帮助的,正如@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

0
部分解决方案:在指定的工作区中打开Eclipse:
eclipse.exe -data c:\code\workspace-name

如上所述,我已经可以做到这一点,但这并没有太大帮助。我仍然需要关闭之前的项目并通过导入来打开新项目。 - alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接