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

ale*_*lex 6 eclipse

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

Ric*_*ler 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
Run Code Online (Sandbox Code Playgroud)

祝好运!

/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
Run Code Online (Sandbox Code Playgroud)

/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>
Run Code Online (Sandbox Code Playgroud)

/.项目:

<?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>
Run Code Online (Sandbox Code Playgroud)

/.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>
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

/ 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));
    }
}
Run Code Online (Sandbox Code Playgroud)


Pau*_*and 5

这个问题上给出了另一种可能的选择.答案的实质是,如果你安装了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}
Run Code Online (Sandbox Code Playgroud)

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