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:

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[]{

    public String[] getSupportedActions() {
        return supported;

    public void invokeAction(String string, Lookup lookup) throws IllegalArgumentException {
        if (string.equalsIgnoreCase(ActionProvider.COMMAND_DELETE)) {
        if (string.equalsIgnoreCase(ActionProvider.COMMAND_COPY)) {
        //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) {

    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:

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.


Three cheers for Gj, hip-hip hurray :D

Thanks a lot.

Posted by Varun Nischal on May 08, 2009 at 12: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 02:58 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« November 2015