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。
二、如何访问current project:
上一节给出来在eclipse plugin 中访问eclipse workspace, 从而访问该workspace下所有project的方案,WorkSpace以及相关的类不提供直接访问current project的方法,所以只能走其他途径.
在我们的plugin中,我们要提供界面入口,比如 PopMenu ActionMenu 等之类的,
这些界面入口是要实现一些接口的,例如:PopMenu要实现IObjectActionDelegate,
这个接口有几个方法,其中 public void selectionChanged(IAction action, ISelection
selection) ;
这个方法很早重要,可以通过ISelection获得当前选择中的Project.
ISelection共有三个子接口,分别对应三个实现类,那么通过判断ISelection的实际类型可以获得其子接口的引用,
然后对其遍历,通过getAdaptor方法获得所有的选择的IResource的引用,
再进一步对IResource进行类型识别,得到IResource.PROJECT类型的元素即为IProject的引用.
下面是程序:
import java.lang.reflect.Array;import java.util.ArrayList;