Simple shell for Ant on Windows

AntBatch scripts

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.

Comments

Leave a Reply