Saturday 24 February 2007

JDeveloper script mastery with Ant

In my earlier days as a C++ (cough) newbie programmer on a real-time database system (a long story - Oracle just wasn't fast enough apparently), I had the pleasure of working with a team of C++ UNIX gurus. When I mean gurus I mean gurus with a capital URU.

You'll know these sort of guys if you ever meet one. Most have the ability to debug the UNIX kernel using a tuning fork sitting on the disk platter. They also as a rule love Emacs and its serious macro scripting facilities. They wouldn't think twice of writing an Emacs script to turn on the coffee pot or change the weather.

As I was tone deaf I decided to leave these gurus and stick with Oracle instead, and never looked back .... until now where I've been hacking away with C++'s teenage monster Java for the last few years.

JDeveloper, my IDE of choice, is no Emacs (thank goodness). But in a nod to my C++ colleagues of past, what sort of scripting can we do in JDeveloper? Ant comes to the rescue.

Ant is an XML cross-platform based scripting language for building applications in an automated fashion. Imagine you want to write a script to compile your Java program and FTP it to a server in an automated manner, Ant is the tool to use.

Ant has become such a powerful little beastie that it goes beyond builds, and here's a little hack I like to use which hopefully will highlight its usefulness.

On my Windows XP laptop I don't let Windows services start my hack-and-slash (aka development) Oracle 10g Enterprise Edition database automatically as it takes up far too many resources. However once JDev has started it goes hand-in-hand that I'd like the database started too. Starting the RDBMS via the Windows admin tools is far too many mouse clicks, and writing a batch file on my filesystem that I loose every time I rebuild my lappy doesn't work either.

However as I'm always working in JDev, I can write an Ant script to do the same thing.

As you already know, the Windows services required to run the Enterprise Edition of the RDBMS are:

  • OracleServiceXX (where XX is the RDBMS service name)
  • OracleOraDb10g_home1TNSListener

To start these from DOS you can use the command:

net start (servicename)

To start the services in Ant and JDeveloper, we use JDeveloper to create the basic Ant script file called build.xml. This can be created via the New Gallery selecting All Technologies -> General -> Ant -> Empty Buildfile.

By default within JDeveloper's Application Navigator the build file is placed under the Resources node of your current project.

The XML Ant buildfile I use to start and stop the RDBMS looks as follows:

<?xml version="1.0" encoding="windows-1252" ?>
<project>
  <target name="startDB">
    <exec executable="net">
      <arg value="start"/>
      <arg value="oracleservicexx"/>
    </exec>
    <exec executable="net">
      <arg value="start"/>
      <arg value="oracleoradb10g_home1tnslistener"/>
    </exec>
  </target>
  <target name="stopDB">
    <exec executable="net">
      <arg value="stop"/>
      <arg value="oracleoradb10g_home1tnslistener"/>
    </exec>
    <exec executable="net">
      <arg value="stop"/>
      <arg value=" oracleservicexx"/>
    </exec>
  </target>
</project>

(Again were oracleservicexx is your actual db service name)

Note that the buildfile is comprised of a project, and two targets startDB and stopDB. A target is a named runnable part of your buildfile. JDeveloper has out of the box support to run the specific targets by right clicking on the build.xml file, selecting Run Ant Target, then selecting the particular target you wish to run in the submenu.

Within each target there is an exec tag to execute an external command. In this case you'll note I'm calling the Windows net command, passing in two arguments with the arg tag.

That's it. It is a very simple example but again a useful little hack. Once you execute the target, the Ant output is logged to JDeveloper's log window.

As you can imagine you can use Ant for all sorts of house keeping exercises. Rather than doing all the house work yourself, let Ant do the work for you. So no longer are DBAs the script masters, developers can be too.

No comments: