在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令、访问eclipse workspace中的project等。一次在网上偶遇到本文的原创者kobye,此人正在进行jsports项目的开发,对此颇有心地,故在此行文与众人共同探讨之。
一、基础工作-在插件中以编程的方式调用ant命令:
在开发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();