Simple shell for Ant on Linux

As promised in Simple shell for Ant on Windows i would provide a Linux version of the simple Ant shell. So here is a version based on the Bash shell. I’ve written it using Cygwin, but using it on Linux should not be much of a problem. Of course this Linux version has the same functionality as the Windows version.

#!/bin/bash

project=build.xml

ANT_HOME="/cygdrive/c/programs/apache-ant-1.7.1"

if [ ! -d "$ANT_HOME" ]
then
	echo Error: ANT_HOME does not have a valid value
	read
	exit -1
fi

function help
{
	$ANT_HOME/bin/ant.bat -p -f $project
	echo ""
	echo "Other commands:"
	echo ""
	echo " ?      Show this help."
	echo " help   Show this help."
	echo " q      Stop this shell."
	echo " quit   Stop this shell."
	echo " exit   Stop this shell."
	echo ""
}

# clear the terminal
echo -e "\0033\0143"

# set the terminal title 
echo -en "\033]2;env - A sample Ant shell\007"

echo "For more information/help, type '?' or 'help'."

while true
do
	read -p " $ " command
	
	if [ "$command" == "?" ] || [ "$command" == "help" ]
	then
		help
	else if [ "$command" == "q" ] || [ "$command" == "quit" ] || [ "$command" == "exit" ]
	then
		exit 1
	else
		$ANT_HOME/bin/ant.bat "$command" -f $project -propertyfile env.properties 
	fi
	fi
done

Simple shell for Ant on Windows

Most of the time an Ant target is started from Eclipse, but i wanted to use Ant on systems without Eclipse too. So i created a script. This Windows script is a simple shell to start Ant targets. This script saves you from writing multiple specific scripts for each target. With this script you can just type the target name and it will be executed. So how advanced this is, depends mostly on the targets in your build.xml.

There are some prerequisites. Ant is needed of course. Ant itself needs the tools.jar for the Java SDK. So that’s needed too. The batch file is kept as simple as possible. It only contains commands for help and closing the shell. All the other commands come from the build.xml. Check the supplied build.xml for samples.

Put all the three files below in one directory to make it work. A Linux version of this script will be available soon.

env.bat

@echo off

set project=build.xml

if not exist "%ANT_HOME%" goto :anthomenotvalid
if not exist "%JAVA_HOME%" goto :javahomenotvalid

REM make sure this script runs from the directory it's located in
REM change drive first, then change directory
%~d0 
CD %~p0 

cls
title env - A sample Ant shell
echo For more information/help, type '?' or 'help'.

:start
set /p command= $ 

if "%command%" == "?" goto help
if "%command%" == "help" goto help

if "%command%" == "q" goto end
if "%command%" == "quit" goto end
if "%command%" == "exit" goto end

call %ANT_HOME%\bin\ant.bat "%command%" -f %project% -propertyfile env.properties

goto start

:end
exit

:anthomenotvalid
echo Error: ANT_HOME does not have a valid value
pause
exit

:javahomenotvalid
echo Error: JAVA_HOME does not have a valid value
pause
exit

:help
call %ANT_HOME%\bin\ant.bat -p -f %project%
echo.
echo Other commands:
echo.
echo  ?      Show this help.
echo  help   Show this help.
echo  q      Stop this shell.
echo  quit   Stop this shell.
echo  exit   Stop this shell.
echo.
goto start

build.xml

<project name="Sample Ant file" basedir=".">
  
	<property environment="env"/>
  
	<target name="hello" description="This will show hello world.">
		<echo>Hello, world!</echo>
	</target>
	
	<target name="myproperty" description="Show the value of myproperty.">
		<echo>${myproperty}</echo>
	</target>
	
	<target name="hidden">
		<echo>This doesn't show in the because it has no description</echo>
	</target>
	
	<target name="properties" description="Show the values of all properties available.">
		<echoproperties />
	</target>
	
</project>

env.properties

myproperty=this is some custom value

UPDATE 20-7-2011:
The Linux version is available here.