Thursday, July 22, 2010

Args4j, Netbeans, and Ant Oh My!

Args4j under netbeans


For some reason Args4j does not work with the simulated command line under netbeans. The application must be deployed for the library to work properly. A controlling shell script needs to wrap the call into the jar file holding the Java based command. The best place to keep this script file is the top directory of the project under netbeans. Netbeans performs no modification of this file. It just needs to have the .sh extension stripped when fully deployed and the file permissions set to execute. Rather than look for a plugin it seems that some simple modification of the ant build script was in order.

Ant is an xml based scripting language helpful in the processing of java builds. Its input file is called “build.xml” highlighting its role in the build process. The file contains “targets” and “tasks”. The tasks are the low level ant commands used to move files, compile files, or create archives. “targets” are the goals of the build process. Each target maintains a dependency list . Ant will check through the dependency list to see if anything prior in the dependency chain of a target has changed and will then execute all targets necessary to provide the latest changes to the build process.

Ant and Netbeans


Every Netbeans project has a build.xml file in the project directory. It imports all of its targets from a prepackaged ant script tailored for the netbeans environment. This means that there is basically nothing in this build.xml other than a few comments to tell the developer how to modify it.

The shell script source file needs to be placed in the distribution directory. The building process under ant knows all about how to make jar files compiled from the Java source code and copy them to the appropriate location in the distribution directory of the project (aptly named “dist”). But it really doesn't know about shell scripts associated with that source code (at least I am unaware if it does have a way of handling it). Ant needs to be told to move the script file from its location at the top of the project directory into the distribution directory. This operation must be performed every time the distribution directory is created. This means that one of the predefined targets netbeans provides is an appropriate place to put the code to move the script file.

The ant target looks like this:
< target name="-post-jar" >
< copy todir="${basedir}/dist">
< fileset dir="${basedir}" includes="*.sh"/>
< /copy>
< /target>

This target automatically gets run after the jar file for the project has been built and placed in the dist directory. The .sh on the shell script needs to come off but that will happen later. The .sh extension will uniquely identify new shell script commands during the deployment phase. This will distinguish them from already existing commands in the $HOME/bin directory.

The -post-jar target is an empty target created for netbeans ant scripts so the user can override it and place some specialized build handling instructions there. It is perfect for the purposes of moving files that netbeans doesn't really know about but are necessary for the execution of this java command.

Once the distribution directory is properly populated a custom target can be built to move the executable code and script file to $HOME/bin or other such user command directory. A custom target in the build.xml file called deploy-home:
< target name="deploy-home" depends="-post-jar">
< copy todir="${env.HOME}/bin">
< fileset dir="${basedir}/dist" excludes="README.*"/>
< /copy>
< chmod perm="777" type="file">
< fileset dir="${env.HOME}/bin">
< include name="*.sh"/>
< /fileset>
< /chmod>
< move todir="${env.HOME}/bin">
< fileset dir="${env.HOME}/bin">
< include name="*.sh"/>
< /fileset>
< mapper type="glob" from="*.sh" to="*"/>
< /move>
< /target>

The above target accomplishes 3 things. It copies files from <project>/dist to $HOME/bin. It then sets the permissions of the executable shell scripts. It then strips the .sh extension using the “move” task. Targeting a fileset of all .sh files. In this manner only newly deployed shell scripts are the target of the “chmod” task.

Executing a custom ant target in Netbeans


In the netbeans GUI on the left side there are a number of tabs: one for projects and one for files are there. By clicking on the files tab a new file tree view is displayed and by clicking on the project of interest the build.xml for that project can be exploded. This file viewer tab knows about ant build.xml files and will explode all the available targets. So after hand editing build.xml and adding the custom target “deploy-home” it will now appear as one of the targets in this netbeans exploded view. Right clicking the mouse on this task will bring up a pop-up menu, select Run Target and ant will perform the commands enclosed in the target definition. In the case above everything in the directory “dist” will be copied to $HOME/bin.

No comments:

Post a Comment