Download NetBeans!

20090508 Friday May 08, 2009

Build Project Command for Ant Based Project Types

The whole point of working with the @AntBasedProjectRegistration annotation at all is that you'd like the user to be able to invoke Ant targets from the Ant script belonging to the project for which you're creating the annotation:

So, how to add the "Build Project" menu item to the project's contextual menu? Firstly, add the line below to the getActions() in the LogicalViewProvider:

@Override
public Action[] getActions(boolean arg0) {
    Action[] nodeActions = new Action[7];
    nodeActions[0] = CommonProjectActions.newFileAction();
    //The 'null' is a reference to no properties being used, in this case.
    nodeActions[1] = ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_BUILD, "Build Project", null);
    nodeActions[2] = CommonProjectActions.copyProjectAction();
    nodeActions[3] = CommonProjectActions.deleteProjectAction();
    nodeActions[5] = CommonProjectActions.setAsMainProjectAction();
    nodeActions[6] = CommonProjectActions.closeProjectAction();
    return nodeActions;
}

Next, you need an ActionProvider, such as the following, which includes the Build command:

private final class ActionProviderImpl implements ActionProvider {

    private String[] supported = new String[]{
        ActionProvider.COMMAND_DELETE,
        ActionProvider.COMMAND_COPY,
        ActionProvider.COMMAND_BUILD,
    };

    @Override
    public String[] getSupportedActions() {
        return supported;
    }

    @Override
    public void invokeAction(String string, Lookup lookup) throws IllegalArgumentException {
        if (string.equalsIgnoreCase(ActionProvider.COMMAND_DELETE)) {
            DefaultProjectOperations.performDefaultDeleteOperation(DemoProject.this);
        }
        if (string.equalsIgnoreCase(ActionProvider.COMMAND_COPY)) {
            DefaultProjectOperations.performDefaultCopyOperation(DemoProject.this);
        }
        //Here we find the Ant script and call the target we need!
        if (string.equalsIgnoreCase(ActionProvider.COMMAND_BUILD)) {
            try {
                FileObject buildImpl = helper.getProjectDirectory().getFileObject("nbproject/build-impl.xml");
                ActionUtils.runTarget(buildImpl, new String[]{"compile"}, null);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    @Override
    public boolean isActionEnabled(String command, Lookup lookup) throws IllegalArgumentException {
        if ((command.equals(ActionProvider.COMMAND_DELETE))) {
            return true;
        } else if ((command.equals(ActionProvider.COMMAND_COPY))) {
            return true;
        } else if ((command.equals(ActionProvider.COMMAND_BUILD))) {
            return true;
        } else {
            throw new IllegalArgumentException(command);
        }
    }
}

Finally, add the ActionProvider to the Lookup of your Project:

@Override
public Lookup getLookup() {
    return Lookups.fixed(new Object[]{
                new Info(),
                new DemoProjectLogicalView(this),
                new ActionProviderImpl()
    });
}

And that's all. You've now added a Build Project command to your project type. When the user selects it, the "compile" target is invoked in the Ant script. In the same way, you can add others, such as "Clean" and "Run" or whatever you want to expose to the user of your project type.

May 08 2009, 05:30:55 AM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/build_project_command_for_ant
Comments:

Three cheers for Gj, hip-hip hurray :D

Thanks a lot.

Posted by Varun Nischal on May 08, 2009 at 07:20 AM PDT #

The null arg to PSA.pCA says to use a default icon. It has nothing to do with "properties".

Use equals, not equalsIgnoreCase.

Note that it would be conventional to use build.xml rather than nbproject/build-impl.xml as the script to run. Of course this depends entirely on what kinds of scripts you create and how you maintain them.

Posted by Jesse Glick on May 13, 2009 at 09:58 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed