我正在为类似于 Java 的自定义语言创建 Eclipse 插件。目前我制作了一个编译器插件(与 eclipse.jdt.core 相同)并从中创建了 jar 文件。我还为我的项目创建了将以该语言编写的自定义性质。有没有办法实现像Java项目一样自动构建这个项目?我想以某种方式将我的编译器插件与我的项目类型相关联。
通过添加这个编译器插件,我实现了代码完成、语法突出显示,并且我可以编译项目,但我似乎找不到使其自动编译的选项。我所说的自动编译是指每当文件发生更改时,它都会将其重新编译为 bin 目录中的 .class 文件。
最佳答案
使用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);
另请参阅Incremental Builder在 Eclipse 帮助中。
关于java - 在 Eclipse 中自动编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45970353/