在 Eclipse 中自动编译

Autocompile in Eclipse

我正在为类似于 Java 的自定义语言创建 Eclipse 插件。目前我制作了一个编译器插件(与 eclipse.jdt.core 相同)并从中创建了 jar 文件。我还为将以该语言编写的项目创建了自定义性质。有没有办法像 Java 个项目一样自动构建这个项目?我想以某种方式将我的编译器插件与我的项目类型相关联。

通过添加这个编译器插件,我实现了代码补全、语法高亮,我可以编译项目,但我似乎找不到让它自动编译的选项。通过自动编译,我的意思是每当文件被更改时,它都会将其重新编译为 .class bin 目录中的文件。

使用 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