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 the .java source files into the .class files. Perform the following steps:

  1. Open the minimark.ui project's .project file. This is visible in the Navigator view, but not in the Package Explorer or other views. Builders are associated to a project within the .project file. The builder ID is referenced via buildCommand, for example have a look at the following code:
    <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 asks that the full project is built. Since this is defined in the core resources bundle, open plugin.xml and add the org.eclipse.core.resources bundle to plugin.xml in 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 of flag, which indicates whether the entire project is being built, or if 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 non-free operation so should only be done 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 case where getDelta() returns a null value, and invoke the full builder accordingly:
    private void incrementalBuild(IProject project, IProgressMonitormonitor, 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 IncrementalProjectBuilder. Add the following code to plugin.xml:
    <extension id="MinimarkBuilder"point="org.eclipse.core.resources.builders"><buildercallOnEmptyDelta="false"hasNature="false"isConfigurable="false"supportsConfigurations="false">
        <run class="com.packtpub.e4.minimark.ui.MinimarkBuilder"/>
      </builder>
    </extension>

    Note

    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 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. 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 standard pattern which allows an implementation to determine if 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