Thursday April 07, 2005
Tomcat 4, JBoss, and Ant in the NetBeans IDE
So now I have an Ant properties file with the following content:
# Tomcat 4
#port.number=8085
#build.web.dir=c:/Program Files/Apache Group/Tomcat 4.1/webapps/${application.name}
#application.name=WebApplication1
# JBoss
port.number=8082
build.web.dir=C:/jboss/jboss-4.0.1sp1/server/default/deploy/${war.name}
application.name=WebApplication1
war.name=WebApplication1.war
And my Ant script contains one target:
<target name="aaa_Deploy-To-Tomcat4-or-JBoss" description="Deploy to Tomcat 4 or JBoss">
<nbbrowse url="http://localhost:${port.number}/${application.name}"/>
</target>
When I import (1) the Ant script containing this target and (2) the file defining the target's properties into a NetBeans IDE project's build.xml file (or whatever the local Ant build file is called), I only have to change the handful of properties in the external Ant file's properties file to be able to deploy to either Tomcat 4 or JBoss, after building the project to the build.web.dir applicable to the server in question. (Of course, I also have targets that start and stop Tomcat 4 and JBoss, but these are IDE-wide targets.) This is how I import the Ant script and properties file (they're both in a folder called "IDETargets", which is a different folder to where my IDE projects are stored -- by storing my re-usable targets in a separate folder I'm protecting them from my occasional over-zealous deletions):
<import file="../../IDETargets/ProjectAntTargets/project-specific-targets.xml"/> <property file="../../IDETargets/ProjectAntTargets/nbproject/project-specific-targets.properties"/>
Here are the 1000 words in a picture:

The cool thing about this is that I now have a separate Ant script that contains all the information I need for deploying to Tomcat 4 or JBoss. Whenever I create a new web application, all I need to do is import the external Ant script and properties file into the new web application's own Ant script, tweak the properties in the external Ant script's properties file, and that's all. No deployment-related information is defined within an individual NetBeans IDE project, and so nothing is lost (and everything is re-usable) when I delete/lose/break an individual NetBeans IDE project.
Apr 07 2005, 08:29:11 AM PDT Permalink
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.org\Mozilla\mozilla.exe">
<arg value="http://localhost:8085/${project.name}/${client.urlPart}"/>
</exec>
</target>
(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"/>
</exec>
</target>
So, to be able to re-use aaa_DeployToTomcat4, I do the following in WebApplication1:
- Add the following to build.xml:
<import file="../ProjectAntTargets/project-specific-targets.xml"/>
- Change the first and add the second property below in the nbproject/project.properties file:
- build.web.dir=c:/Program Files/Apache Group/Tomcat 4.1/webapps/${project.name}
- project.name=WebApplication1
- 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/project.properties file.)
<import file="../WebApplication1/nbproject/project.properties"/>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 project.properties file should be shared as well, but I think it's all pretty cool, however you look at it.
Apr 07 2005, 12:27:50 AM PDT Permalink


