从命令行打开一个特定的eclipse项目 [英] open a specific eclipse project from command line

查看:88
本文介绍了从命令行打开一个特定的eclipse项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

我建议不要这样做,因为使用标准向导。我会专注于关闭非活动项目(见下文)。



编辑:如果您使用ant将项目导入工作区,您可以实现插件做如下面的代码。



你关闭旧项目还是删除它们?我没有任何理由去删除它们。如果您关闭所有不工作的项目(右键单击并选择关闭项目或选择您想要的项目,右键单击 - >关闭无关项目),它们将被平台忽略,因此不会影响到开放的项目。



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






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



将内容放在相关文件中,您可以打包Eclipse插件。我建议使用单独的Eclipse安装(实际上我建议不要使用它),因为它将在工作空间根目录中找到newprojects.txt时运行。



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



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



示例内容:

 #将在工作空间中创建
project1
#将在c:\test\project2
project2 = c:\\test\project2

祝你好运!



/META-INF/MANIFEST.MF:

 清单版本:1.0 
Bundle-ManifestVersion:2
Bundle-Name:Project fettling插件
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。 0encoding =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.0encoding =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.0encoding =UTF-8?> 
< classpath>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER/>
< classpathentry kind =conpath =org.eclipse.pde.core.requiredPlugins/>
< classpathentry kind =srcpath =src / main / java/>
< classpathentry kind =outputpath =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;

/ **
*激活器类控制插件生命周期
* /
public class Activator extends Plugin {

//插件ID
public static final String PLUGIN_ID =name.seller.rich;

//共享实例
private static Activator插件;

/ **
*返回共享实例
*
* @返回共享实例
* /
public static Activator getDefault ){
return plugin;
}

/ **
*构造函数
* /
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 / 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实现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();

文件startupFile = new File(workspaceLocation.toOSString(),
newprojects.txt);

IProgressMonitor monitor = new NullProgressMonitor();

属性属性= new Properties();
if(startupFile.exists()){
properties.load(new FileInputStream(startupFile));
}
if(properties.size()> 0){
//删除现有项目
IProject [] projects = this.workspaceRoot.getProjects();

(IProject项目:项目){
//不要删除内容
project.delete(false,true,monitor);
}

//为(Map.Entry条目:properties.entrySet())创建新项目
{
IProject project = this.workspaceRoot
.getProject((String)entry.getKey());

//插入循环
ProjectDescription projectDescription = new ProjectDescription();
projectDescription.setName((String)entry.getKey());

String location =(String)entry.getValue();

//值将为空String如果行上没有=
//在这种情况下,它将在工作空间中创建
//警告,当前的Windows路径必须转义,
// eg 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(异常e){
IStatus status = new状态(IStatus.INFO,Activator.PLUGIN_ID,
0,无法加载新的项目,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));
}
}


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.

解决方案

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.

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 Explorer view, select Filters... and in the Select the elements to exclude from the view: list check the Closed projects option.


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.

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.

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.

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:

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:

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项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆