12 - Eclipse Builders, Natures and Markers: Page 3 of 3


Natures are used to link project with various functionalities such as builders in a project. For ex: if we add java nature to a project inside eclipse. It associates project with incremental java compiler. natures are added in .project file of the project. Natures are not only used to associate builders but are also used to define many other functionalities for ex: only java nature projects are shown in Package Explorer. Some of the predefined natures are org.eclipse.jdt.core.javanature,org.eclipse.pde.PluginNature.

Reviewing the generated code

Let’s review the plug-in manifest file to see what it takes to create a nature. Nature is created by extending org.eclipse.core.resources.natures extension point. ID is the unique identifier for this nature. Name is the human readable name of the nature.

       Eclipse Plugin creating Nature

Next element is the builder. Here we will add a builder to this nature. We have associated SampleBuilder created earlier with this nature. So that projects with SampleNature use SampleBuilder whenever an XML file is saved.

       Eclipse Plugin defining supertype marker

Next elements which we are going to discuss are runtime and run. Here we will specify a class named Sample Nature which will be used to configure/deconfigure project with Sample Nature. With the help of this class we will be able to configure non Sample Nature project to Sample Nature project at run time. OR we will be able to convert Sample Nature Project to non Sample Nature project. Repercussions of making a Project as Sample Nature project will be that a Sample builder will be attached to project which will run whenever an XML file is saved and will look for any errors in XML.

       Eclipse Plugin running the project


Class com.myplugin.rmp.builder.SampleNature declared above implements IProjectNature interface. It can configure a project with the project nature, or de-configure it. Please refer to Eclipse API Specification to know more about this class. In our example, user is provided with toggle action to convert non Sample Nature project to Sample Nature project. Following method from SampleNature class will be called to associate the project with SampleNature.

public void configure() throws CoreException {
              IProjectDescription desc = project.getDescription();
                       get the description of the project basically .project file information
              ICommand[] commands = desc.getBuildSpec();
                      // get the build commands already associated with project.
              for (int i = 0; i < commands.length; ++i) {
                    if (commands[i].getBuilderName().equals(SampleBuilder.BUILDER_ID)) {
                       return; // Do nothing if Sample builder is already associated with project

               ICommand[] newCommands = new ICommand[commands.length + 1];
                                          // create a new build command
              System.arraycopy(commands, 0, newCommands, 0, commands.length);
              ICommand command = desc.newCommand();
              command.setBuilderName(SampleBuilder.BUILDER_ID); // attach it to sample builder
              newCommands[newCommands.length - 1] = command;
              project.setDescription(desc, null); // write to .project file

Every workbench project contains a .project file that contains build commands. Executing above method causes the following to appear in the buildSpec section of the project's .project file. Also Sample nature is added in natures section.



Like us on Facebook