X

Geertjan's Blog

  • May 8, 2009

Build Project Command for Ant Based Project Types

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 2 )
  • Varun Nischal Friday, May 8, 2009

    Three cheers for Gj, hip-hip hurray :D

    Thanks a lot.


  • Jesse Glick Wednesday, May 13, 2009

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.