使用org.eclipse.core.resources.builders 扩展点来定义增量构建器。当 Eclipse 认为需要构建项目时,例如资源发生变化时,将调用构建器。这是 JDT 构建器声明:
<extension
point="org.eclipse.core.resources.builders"
id="javabuilder"
name="%javaBuilderName">
<builder>
<run class="org.eclipse.jdt.internal.core.builder.JavaBuilder">
</run>
<dynamicReference class="org.eclipse.jdt.internal.core.DynamicProjectReferences"/>
</builder>
</extension>
构建器代码扩展了IncrementalProjectBuilder,大致如下:
public class BuilderExample extends IncrementalProjectBuilder
{
IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException
{
// add your build logic here
return null;
}
protected void startupOnInitialize()
{
// add builder init logic here
}
protected void clean(IProgressMonitor monitor)
{
// add builder clean logic here
}
}
每个项目都有一个与之关联的构建器列表(存储在.project 文件中)。您可以使用 IProjectDescription setBuildSpec 调用添加构建器。这通常在向项目添加性质时完成。比如:
String builderID = ... your builder id
IProject project = ... project
IProjectDescription description = project.getDescription();
ICommand[] oldBuildSpec = description.getBuildSpec();
// TODO check not already present
ICommand newCommand = description.newCommand();
newCommand.setBuilderName(builderID);
// Add a API build spec after all existing builders
ICommand[] newCommands = new ICommand[length + 1];
System.arraycopy(oldBuildSpec, 0, newCommands, 0, length);
newCommands[length] = newCommand;
// Commit the spec change into the project
description.setBuildSpec(newCommands);
project.setDescription(description, null);
另请参阅 Eclipse 帮助中的 Incremental Builder。