Geertjan's Blog

  • June 1, 2005

Ant-Prompted Deployment from NetBeans IDE 4.1

Geertjan Wielenga
Product Manager
Yesterday I found CoolBoy's blog, with an extremely interesting entry called "On the fly" deployment under NetBeans. Before coming across that blog entry, I never knew that Ant has its own input handler, as well as one or two conditional tags. So, using the input handler and the conditional tags, this is the dialog box that appears when I choose "Run Project" in a free-form project's contextual menu:

In standard projects, I could add the target that creates the above dialog box to the -post-dist target, as demonstrated in previous blog entries.

This is the Ant target that creates the dialog box and sets a property depending on which server I select:

<target name="Select-a-Server" description="Select One of 5 Servers">
<input message="Where do you want to deploy to?"
validargs="JBoss,Tomcat,SJS Application Server,WebLogic,WebSphere"
defaultvalue="SJS Application Server"/>
<condition property="choose.JBoss">
<equals arg1="JBoss" arg2="${server.name}"/>
<condition property="choose.Tomcat">
<equals arg1="Tomcat" arg2="${server.name}"/>
<condition property="choose.SJS">
<equals arg1="SJS Application Server" arg2="${server.name}"/>
<condition property="choose.WebLogic">
<equals arg1="WebLogic" arg2="${server.name}"/>
<condition property="choose.WebSphere">
<equals arg1="WebSphere" arg2="${server.name}"/>

And here are two of the five targets for deployment, based on whether a property has been set or not. For example, if the choose.JBoss property is set in the previous target, then -- and only then -- will the Copy-to-JBoss target below be executed. This is all thanks to the if="choose.JBoss" attribute in the target declaration:

<target name="Copy-to-JBoss" depends="Select-a-Server" if="choose.JBoss" description="JBoss">
<copy file="${dist.home}/${app.name}-${app.version}.war" todir="${build.dir.jboss}"/>
<target name="Copy-to-Tomcat" depends="Select-a-Server" if="choose.Tomcat" description="Tomcat">
<copy file="${dist.home}/${app.name}-${app.version}.war" todir="${build.dir.tomcat4}"/>

Finally, to create a single target that bundles the targets above together -- so that they can all be invoked from the same menu item in a free-form project (or as part of the -post-dist target in standard projects) -- the following needs to be added:

<target name="Deploy" depends="Copy-to-Tomcat,Copy-to-JBoss" description="Deploy"/>

That's it. Of course, I haven't added the targets above to the current project's build.xml, but to a project-independent build.xml, which I import into the build.xml of any project that needs it. I think its all pretty cool because this way the same WAR file can be deployed wherever I want, based on a choice I make when prompted by Ant. And everything is invoked from one single menu item in NetBeans IDE 4.1. Or, I can just press F6 to invoke the dialog box. So now, when I set up the IDE as described in a recent blog entry, I just press F6 whenever I want to deploy, I choose my server from the input handler, and then I can view the result in the Favorites window right away (click to enlarge):

You could even go a step further -- append one extra statement to the Copy-to-XXX target and then you'll launch the IDE's default browser and open the deployed application in it. For example, for JBoss, you'd use a target as simple as the following:

<target name="Copy-to-JBoss" depends="Select-a-Server" if="choose.JBoss" description="JBoss">
<copy file="${dist.home}/${app.name}-${app.version}.war" todir="${build.dir.jboss}"/>
<nbbrowse url="http://localhost:${port.number.jboss}/${app.name}-${app.version}"/>

So, the above target is invoked as soon as you select 'JBoss' in Ant's input handler. And, as soon as you've deployed the application to JBoss, you can just go down the list in the input handler and deploy the same WAR file to another server. Each time, you'd see the result straight away because the browser is launched and the application is displayed. Ant rocks. So does NetBeans IDE. Thanks, CoolBoy, for your great tip.

Join the discussion

Comments ( 2 )
  • jim Monday, April 17, 2006
    copy files tools
  • guest Tuesday, August 27, 2013


    I'm begining with ant appache.

    I'd like list a directory and since this list select one or many documents. Like this but it's not ok:

    <foreach target="bar" param="theFile">

    <fileset dir="\\espacecomete\MC2\Rfrentiel documentaire\Arborescence MC2\06_Déploiement\50_Packaging\10_Livraison\${lot}/${date}" casesensitive="yes">

    <include name="**/*.*"/>




    <target name="bar">

    <echo message="${theFile}"/>

    <copy todir="fiche_livraison/" flatten="true"/>

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