【问题标题】:Autocompile in Eclipse在 Eclipse 中自动编译
【发布时间】:2017-08-31 16:56:29
【问题描述】:

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

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

【问题讨论】:

    标签: java eclipse-plugin


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多