@echo off

setlocal

if defined JDK_HOME goto set_java_checkbin
if defined JAVA_HOME goto set_java_java_home
if defined JAVAHOME goto set_java_javahome
if defined JAVA_PATH goto set_java_java_path
goto set_java_error1
:set_java_java_home
set JDK_HOME=%JAVA_HOME%
goto set_java_checkbin
:set_java_javahome
set JDK_HOME=%JAVAHOME%
goto set_java_checkbin
:set_java_java_path
set JDK_HOME=%JAVA_PATH%
goto set_java_checkbin
:set_java_checkbin
set JAVA=%JDK_HOME%\bin\java.exe
if not exist "%JAVA%" goto set_java_error2
goto set_java_success
:set_java_error1
echo Error : JDK_HOME or JAVA_HOME or JAVAHOME or JAVA_PATH does not defined
endlocal
exit /b 1
:set_java_error2
echo Error : java.exe command not found in %JDK_HOME%
endlocal
exit /b 1
:set_java_success

set CURRENT_PATH=%cd%\
set ANT_HOME=%~d0%~p0
set TOOLS_PATH=%ANT_HOME%

rem set absolute directory
cd /d "%ANT_HOME%"
set ANT_HOME=%cd%
cd /d "%TOOLS_PATH%"
set TOOLS_PATH=%cd%
cd /d "%CURRENT_PATH%"

rem set class path
if defined CLASSPATH goto set_class_path_external
  set CP=
  goto set_class_path
:set_class_path_external
  set CP=%CLASSPATH%;
:set_class_path
set CLASSPATH=%CP%%TOOLS_PATH%\junit.jar;%ANT_HOME%\ant.jar;%ANT_HOME%\ant-optional.jar;%JDK_HOME%\lib\tools.jar

rem run ant
java -cp "%CLASSPATH%" -Dant.home="%ANT_HOME%" %ANT_OPTS% org.apache.tools.ant.Main %*

if not errorlevel 0 goto error_exit

endlocal
exit /b 0

:error_exit
endlocal
exit /b 1
