Time for action – building the builder

Compilers (and every other kind of translator) in Eclipse are implemented with builders. These are notified when a file or a set of files are changed, and can take appropriate action. In the case of the Java builder, it translates .java source files into .class files.

  1. Open the .project file in the com.packtpub.e4.minimark.ui project. This is visible in the Navigator view, but not in the Package Explorer or other views. Alternatively, use Cmd + Shift + R on macOS (or Ctrl + Shift + R on other platforms) to open the resource by name. Builders are associated to a project within the .project file. The builder ID is referenced via the buildCommand, for example:
    <projectDescription>
      <name>com.packtpub.e4.minimark.ui</name>
      <buildSpec>
        <buildCommand>
          <name>org.eclipse.jdt.core.javabuilder</name>
        </buildCommand>
          ...
  2. To translate a .minimark file into HTML automatically, a builder is needed. A builder is a class that extends IncrementalProjectBuilder and implements a build method. This is called by the framework when files are saved, and either gives a list of changed files or requests that the full project is built. Since this is defined in the core resources bundle, open the plugin.xml file and add the org.eclipse.core.resources bundle to the dependency list.
  3. Create a class in the com.packtpub.e4.minimark.ui package called MinimarkBuilder:
    public class MinimarkBuilder extends IncrementalProjectBuilder {
      protected IProject[] build(int kind,Map<String, String> args,
       IProgressMonitor monitor) throws CoreException { 
       return null;
      }
    }
  4. Builds are called with different kinds—a flag which indicates whether the entire project is being built, or whether a subset of the project is being built. For builds that aren't FULL_BUILD, there's also a resource delta, which contains the set of resources that have been changed. Calculating a resource delta is a costly operation, so it should only be accessed if needed. The build method is typically implemented as follows:
    protected IProject[] build(int kind, Map<String, String> args,
     IProgressMonitor monitor) throws CoreException {
      if (kind == FULL_BUILD) {
        fullBuild(getProject(), monitor);
      } else {
        incrementalBuild(getProject(), monitor,
         getDelta(getProject()));
      }
      return null;
    }
  5. The fullBuild and incrementalBuild methods need to be defined. It is also necessary to handle the cases where the getDelta method returns a null value, and invoke the full builder accordingly:
    private void incrementalBuild(IProject project, IProgressMonitor
     monitor, IResourceDelta delta) throws CoreException {
      if (delta == null) {
        fullBuild(project, monitor);
      } else {
        System.out.println("Doing an incremental build");
      }
    }
    private void fullBuild(IProject project, IProgressMonitor monitor)
     throws CoreException {
      System.out.println("Doing a full build");
    }
  6. Finally, to hook up a builder, declare its reference in an extension point org.eclipse.core.resources.builders. This defines a reference (via an ID) to a class that implements the IncrementalProjectBuilder. Add the following to the plugin.xml file:
    <extension id="MinimarkBuilder"
     point="org.eclipse.core.resources.builders">
      <builder
       callOnEmptyDelta="false"
       hasNature="false"
       isConfigurable="false"
       supportsConfigurations="false">
        <run class="com.packtpub.e4.minimark.ui.MinimarkBuilder"/>
      </builder>
    </extension>

    Note

    Note that this extension point requires an ID to be given, since the name defined in the .project file will be the plug-in's ID concatenated with the extension ID. It is conventional, but not necessary, for the full ID to be the name of the class.

  7. Run the target Eclipse instance. Create a new General project in the test workspace, and once created, open the .project file. Add the builder manually by adding in a buildCommand with the ID from the extension point:
    <buildSpec>
      <buildCommand>
        <name>com.packtpub.e4.minimark.ui.MinimarkBuilder</name>
      </buildCommand>
    </buildSpec>
  8. Clean the project by going to the Project | Clean menu. The message Doing a full build can be seen in the host console when the builder is added, or if the project is cleaned. Edit and save a .minimark file, and the message Doing an incremental build should be displayed.

What just happened?

The builder is capable of being invoked when files in a project are changed. To associate the builder with the project, it was added as a build command to the project, which is contained in the .project file. The name used for the builder is the extension's unique ID, which is formed as a dot-separated concatenation of the plug-in's ID and the element in the plugin.xml file.

The incremental builder has a kind that allows an implementation to determine whether it is doing a full or incremental build. There is also a clean method (which wasn't implemented here) that is used to remove all resources that have been created by a builder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.73.35