eclipse plugin develop ---how to access eclipse workspace?(eclipse 插件开发)

来源:互联网 发布:淘宝客服投诉电话人工 编辑:程序博客网 时间:2024/05/21 19:23

    在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令等。

   如何做到这一点?

 public void execute(){
     IWorkspace ws = ResourcesPlugin.getWorkspace();
     IProject[] ps = ws.getRoot().getProjects();
     System.out.println(ws.getRoot().getFullPath().makeAbsolute().toOSString());
     for(int i=0;i<ps.length;i++){
         IProject p = ps[i];
         IPath location = p.getLocation();
         IFile ifile = p.getFile("build.xml");
         System.out.println(ifile.getLocation().toFile().getAbsolutePath());      
         File f =  new File(ifile.getLocation().toFile().getAbsolutePath());
         if(!f.exists()){
             continue;
         }
         Project pro = new Project();        
         pro.setBasedir(location.toFile().getAbsolutePath());
         pro.init();
         ProjectHelper helper = ProjectHelper.getProjectHelper();
         helper.parse(pro, f);
         Hashtable tars = pro.getTargets();
         System.out.println("name==="+name);
         Target t = (Target) tars.get(name);
         if(t==null){
             return;
         }
         DefaultLogger consoleLogger = new DefaultLogger();
         consoleLogger.setErrorPrintStream(System.err);
         consoleLogger.setOutputPrintStream(System.out);
         consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
         pro.addBuildListener(consoleLogger);   
         pro.executeTarget(this.name);
         break;
     }
    
 }

 

 

以上代码(单独编译不会通过,请把 name换位ant 的target)可以放到插件的代码中。

以上代码的含义:

    获得eclipse workspace的引用,对workspace下的pronjects进行循环,如果该project下有build.xml并且 该文件中有name的target那么就以ant的方式调用,并把ant运行的输出输出到eclipse的console。

 

原创粉丝点击