Wednesday Apr 06, 2005

Structuring Ant Targets in the NetBeans IDE

I've created one build script for IDE-wide targets (for running and stopping Tomcat 4, for accessing the Tomcat users file for Tomcat 5.5, and for easy access to Star Office) and a separate build script for project-specific targets. I'm keeping these two build scripts separate so that (1) I know where to look for my targets and (2) I only need to import the project-specific build script into any project-level build script that might need it.

So, for example, in the illustration below, I only need to import project-specific-targets.xml into a project's build.xml file, because the IDE-wide targets are irrelevant on project-level. The aaa_DeployToTomcat4 task is quite useful because the IDE does not officially support Tomcat 4, but thanks to Ant-integration anything can be integrated into the IDE. (I've prefaced the deployment target for Tomcat 4 with "aaa_" so that it ends up at the top of the list in the build.xml files that import it):

This is what aaa_DeployToTomcat4 looks like:

<target name="aaa_DeployToTomcat4" description="Deploy to Tomcat 4">
    <exec executable="C:\\Program Files\\\\Mozilla\\mozilla.exe">
	<arg value="http://localhost:8085/${}/${client.urlPart}"/>

(When I installed Tomcat 4, I set the port number to 8085, which is easy to remember because it's just after the IDE's bundled Tomcat's port number 8084, and because Tomcat's default port number is 8080 which is the same as the Sun Java System Application Server.)

And, by the way, this is how I start Tomcat 4 (RunTomcat4 in ide-wide-targets.xml):

<target name="RunTomcat4">
    <exec executable="c:\\Program Files\\Apache Group\\Tomcat 4.1\\bin\\startup.bat">
        <env key="CATALINA_HOME" value="c:\\Program Files\\Apache Group\\Tomcat 4.1"/>
        <env key="CATALINA_BASE" value="c:\\Program Files\\Apache Group\\Tomcat 4.1"/>
So, to be able to re-use aaa_DeployToTomcat4, I do the following in WebApplication1:

  1. Add the following to build.xml:

    <import file="../ProjectAntTargets/project-specific-targets.xml"/>
  2. Change the first and add the second property below in the nbproject/ file:
    • build.web.dir=c:/Program Files/Apache Group/Tomcat 4.1/webapps/${}

  3. Right-click the project node, choose Properties, click Run, and type the starting page in the Relative URL field. (This field sets the client.urlPart property in the nbproject/ file.)
Finally, you must make sure that the properties set in nbproject/ are available to aaa_DeployToTomcat4, so add this to the top of the aaa_DeployToTomcat4 target:

<import file="../WebApplication1/nbproject/"/>
And then you can create shortcuts and deploy to Tomcat 4 using the reusable aaa_DeployToTomcat4 target. (And when I delete WebApplication1, as I am bound to do sooner or later, I'll not destroy aaa_DeployToTomcat4, because it's in a separate build script.) There's probably better ways of doing this. For example, maybe the file should be shared as well, but I think it's all pretty cool, however you look at it.

Sharing Ant Targets Across NetBeans IDE Projects

Some targets are not project-specific, but IDE-generic. For example, the target created in the previous entry gives one-click access to the tomcat-users.xml file from the IDE. This file shouldn't only be available from a specific project. I shouldn't have to cut and paste the target into each project's build script (neither should I have to run the target in one build script while working in another). So I've created a separate build script where I'll store all targets that are useful for all projects. Now, all I need to do is add the following line to one or more project's build scripts:
<import file="path-to-build-file/ide-generic-targets.xml"/>
Then, when I build the project, the targets from the IDE-generic build script are imported into the project's project-specific build script. It also means that when I delete a project, I won't be deleting a target that other projects (now or in the future) might find useful. I guess this solution is pretty obvious, but it took me a while to think of it...

NB: Maybe an even better solution is to create a web free-form project with a build script containing all the IDE-generic targets. That project could then have all these targets mapped to project commands, with shortcuts like buttons and menu items for actions such as opening external applications that are useful for all projects. Yup. That works. And is a much better solution:


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.


« April 2005 »