IProject中的Builder与Nature
1.创建一个新的工程
如下是创建一个新的工程的代码,但是此时工程就是一个文件夹,没有任何特性
- publicvoid createProject(String projectName)throws CoreException{
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IWorkspaceRoot root = workspace.getRoot();
- IProject project = root.getProject(projectName);
- if(!project.exists()){
- project.create(null);
- project.open(null);
- }
- ProjectUtil.addNature2Project(project, new String[]{LengProjectNature.ID},null);
- }
public void createProject(String projectName) throws CoreException{
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject(projectName);
if(!project.exists()){
project.create(null);
project.open(null);
}
ProjectUtil.addNature2Project(project, new String[]{LengProjectNature.ID}, null);
}
2.为project扩展相应的nature
nature是工程特性的一种标识,eclipse中有如此多种类的project,系统是如何识别他们的。IProject对象本身没有什么特别的标识,所以区分各种不同的project,主要是nature来进行区分。例如,如果两种不同的project对象,如果有同样的一种nature特性,如果我们只需要考虑这种nature特性的时候,这两种project其实是可以等同的。可以借鉴equals方法来进行理解。
- <extension
- id="com.leng.test.project.nature.LengProjectNature"
- name="Leng Project Nature"
- point="org.eclipse.core.resources.natures">
- <runtime>
- <run
- class="com.leng.test.project.nature.LengProjectNature">
- </run>
- </runtime>
- <builder
- id="com.leng.test.project.nature.LengIncrementalProjectBuilder">
- </builder>
- </extension>
<extension
id="com.leng.test.project.nature.LengProjectNature"
name="Leng Project Nature"
point="org.eclipse.core.resources.natures">
<runtime>
<run
class="com.leng.test.project.nature.LengProjectNature">
</run>
</runtime>
<builder
id="com.leng.test.project.nature.LengIncrementalProjectBuilder">
</builder>
</extension>
1)一个nature里面可以包含多个builder,并且用builder的id进行识别
注意:com.leng.test.project.nature.LengIncrementalProjectBuilder是builder所在的插件的id+.+builderId
2)实现一个IProjectNature类
- publicclass LengProjectNatureimplements IProjectNature {
- private IProject project;
- /**
- * nature的ID为plugin的ID加上nature扩展的ID
- */
- publicstaticfinal String ID ="com.leng.test.project.com.leng.test.project.nature.LengProjectNature";
- @Override
- publicvoid configure()throws CoreException {
- ProjectUtil.addBuilderToProject(project,
- new String[] { LengIncrementalProjectBuilder.ID },null);
- }
- @Override
- publicvoid deconfigure()throws CoreException {
- ProjectUtil.removeBuilderFromProject(project,
- new String[] { LengIncrementalProjectBuilder.ID },null);
- }
- @Override
- public IProject getProject() {
- return project;
- }
- /**
- * setProject()和configure()的调用顺序是先调setProject()后调configure().
- */
- @Override
- publicvoid setProject(IProject project) {
- this.project = project;
- }
- }
public class LengProjectNature implements IProjectNature {
private IProject project;
/**
* nature的ID为plugin的ID加上nature扩展的ID
*/
public static final String ID = "com.leng.test.project.com.leng.test.project.nature.LengProjectNature";
@Override
public void configure() throws CoreException {
ProjectUtil.addBuilderToProject(project,
new String[] { LengIncrementalProjectBuilder.ID }, null);
}
@Override
public void deconfigure() throws CoreException {
ProjectUtil.removeBuilderFromProject(project,
new String[] { LengIncrementalProjectBuilder.ID }, null);
}
@Override
public IProject getProject() {
return project;
}
/**
* setProject()和configure()的调用顺序是先调setProject()后调configure().
*/
@Override
public void setProject(IProject project) {
this.project = project;
}
}
3.nature的img扩展:
- <extension
- point="org.eclipse.ui.ide.projectNatureImages">
- <image
- icon="com.leng.test.project.image1"
- id="com.leng.test.project.image1"
- natureId="com.leng.test.project.image1">
- </image>
- </extension>
<extension
point="org.eclipse.ui.ide.projectNatureImages">
<image
icon="com.leng.test.project.image1"
id="com.leng.test.project.image1"
natureId="com.leng.test.project.image1">
</image>
</extension>
依赖插件org.eclipse.ui.ide,此扩展点为特点的nature项目提供特点的图像标记,如java工程中的文件中“J”。
4.为nature扩展相应的builder
刚才说到nature是用来标识project的特性的,但是特性不能当饭吃。new一个工程里面那么多东西,而且算不是new,是其他改变工程里面资源的操作。如果说nature只是标识,那么builder就是实实在在的构建了。
当然,构建器本身是独立的,出来跟nature绑定使用,还可以直接添加到project中
- <extension
- id="com.leng.test.project.nature.LengIncrementalProjectBuilder"
- point="org.eclipse.core.resources.builders">
- <builder
- callOnEmptyDelta="false"
- hasNature="true"
- isConfigurable="true">
- <run
- class="com.leng.test.project.nature.LengIncrementalProjectBuilder">
- </run>
- </builder>
- </extension>
<extension
id="com.leng.test.project.nature.LengIncrementalProjectBuilder"
point="org.eclipse.core.resources.builders">
<builder
callOnEmptyDelta="false"
hasNature="true"
isConfigurable="true">
<run
class="com.leng.test.project.nature.LengIncrementalProjectBuilder">
</run>
</builder>
</extension>
hasNature:该构建器是否跟nature关联,默认为false,如果为true则必须nature存在才会触发构建
isConfigurable:设定构建器是否可以通过特定的构建触发器运行,如果为true,则可以通过ICommand.setBuilding
callOnEmptyDelta:当增量构建的增量为null时,如果设置此属性为true则始终构建,不会关构建内容是否为null。
- publicclass LengIncrementalProjectBuilderextends IncrementalProjectBuilder {
- publicstaticfinal String ID ="com.leng.test.project.nature.LengIncrementalProjectBuilder";
- public LengIncrementalProjectBuilder() {
- }
- @Override
- protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
- throws CoreException {
- getCommand();
- getDelta(getProject());
- switch (kind) {
- case FULL_BUILD:
- // processFullBuild(monitor);
- break;
- case CLEAN_BUILD:
- break;
- default:
- IResourceDelta delta = getDelta(getProject());
- break;
- }
- returnnull;
- }
- @Override
- protectedvoid startupOnInitialize() {
- super.startupOnInitialize();
- }
- /**
- * 清除一些保留且无用的东西
- */
- @Override
- protectedvoid clean(IProgressMonitor monitor)throws CoreException {
- super.clean(monitor);
- }
- }
public class LengIncrementalProjectBuilder extends IncrementalProjectBuilder {
public static final String ID = "com.leng.test.project.nature.LengIncrementalProjectBuilder";
public LengIncrementalProjectBuilder() {
}
@Override
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException {
getCommand();
getDelta(getProject());
switch (kind) {
case FULL_BUILD:
// processFullBuild(monitor);
break;
case CLEAN_BUILD:
break;
default:
IResourceDelta delta = getDelta(getProject());
break;
}
return null;
}
@Override
protected void startupOnInitialize() {
super.startupOnInitialize();
}
/**
* 清除一些保留且无用的东西
*/
@Override
protected void clean(IProgressMonitor monitor) throws CoreException {
super.clean(monitor);
}
}
kind:构建类型,FULL_BUILD(全量构建),INCREMENTAL_BUILD(增量构建),
AUTO_BUILD(自动构建,也是增量的),CLEAN_BUILD(在调用build方法前调用clean方法)
getDelta:获取指定工程里面增量改变的资源内容
派生资源:派生资源就是完全由build构建出来的资源,由某一种源派生出来。
5.构建器的触发
构建器的触发有两种方式:一种是自动触发,系统资源改变时,由eclipse自动增量构建。另一种是手动构建,IWorkspace和IProject里面都有构建方法,只要获取到相关对象就可以进行构建。