Tuesday, November 18, 2008

100 Ant day revised after class

A rebuild of last week's lab —

See below an addition made after class meeting on November 18.

Today's work is about automating builds. We will consider building both from the command line and the method used in IDEs such as NetBeans, and we will examine customary directory structures for Java programming.
  1. Last week we talked about make — old school again! — and XML, which is not old school.


  2. Ant is a program from the Apache Software Foundation, an outfit that makes open-source software. Ant reads a script called build.xml for instructions on compiling and packaging your Java project. (It can be used in other programming environments, too.) This straightforward article discusses first steps in Ant, building — natch — Hello, World.

    Carry out this Hello project on the wocsc Linux server. You should find SecureShell ready to go on your desktop.

    • From the prompt, do ant -version to ensure that Ant is there and which ant to see where it is.

    • Ant expects certain environment variables. Set them in this fashion:

      export ANT_HOME=/usr/bin/ant
      export JAVA_HOME=/usr/java/jdk1.6.0
      export PATH=${PATH}:${ANT_HOME}/bin
      
      Note that there are no slashes at the ends of the environment variables. Also, you might add these commands to your .bashrc so they'll already be there.

    • Be sure to give due attention to the commands for construction of directories in the Hello doc.

    • Note the targets, too.

    • Note that in the bash shell, the command to make a new directory is mkdir.

    Please do everything up through "Using external libraries."

  3. Next, the same thing on Windows — from the command line.
    • Look for Ant in your c:Program Files\NetBeans\java2 directory.

    • Make a little batch file in your working directory — antprep.bat would be an OK name — that sets environment variables:
      set ANT_HOME=path_to_Ant_top
      set JAVA_HOME=path_to_Java
      set PATH=%PATH%;%JAVA_HOME%;%ANT_HOME%\bin
      

    • Addition:
      Ant expects short, old-fashioned file and directory names with no more than 8 + 3 characters in the environment variables ANT_HOME and JAVA_HOME. Note that these environment variables are used by Ant, not required by Java or the OS.

      For example, the home directory on the machine I'm using now has two names, both unique: "C:\Documents and Settings\vfitton" or "C:\DOCUME~1\VFITTON". Every pathname on the system has to be unique. Windows uses twiddles and digits to make short names for backwards compatibility with legacy programs. (I have no idea why Ant insists on them, too.)

      Find out what the short appropriate pathnames to your ANT_HOME and JAVA_HOME are by using the command shell (from the Run command) rather than cmd and then crawling along the paths from root to finish.

    • Carry on with the entire Hello project through "Using external libraries."

  4. This tutorial from TechTracer is more complex than what we need, but the directory structure that the writer proposes will interest you.

    Notice what look like OS command shell commands are embedded in the Ant script, in the form <command />, about halfway down the page. They're actually Ant commands — the article at this JavaWorld link lists all of them.

  5. Break time: Check out this great Java resources page at IBM developerWorks. As the man says, "Information overload is a real problem for Java language developers."

  6. Speaking of information overload, you will love Refcardz.

  7. When you use NetBeans, it builds and runs an Ant file for you. Do the same Hello in NetBeans and then compare the Ant result with what you did by hand. (You need to understand it both ways.)
    • NetBeans creates files that are about your program rather than a part of it; this is called metadata. You can see such files in the Files window rather than the Project window.

    • You will find build.xml much shorter than you expect.

    • There are other .xml files in your project's nbproject folder. Check them out, too. Observe the targets, the variables created, and the dependencies. Lots to learn!

  8. Examine the structure and contents of the Ant folder. Read its batch files.

  9. Try the Linux make on a simple C++ program. Here are a how-to and a what.

No comments: