Search code examples
javaantbuildjarbuild-process

How to run `ant` build-script without setting any environment variable?


I would like to run the ant build-script without installing Java and setting any environment variable like path, JAVA_HOME & ANT_HOME to environment variable.

I have copied already installed folders of Jdk-1.7u17, Jre-1.7u17 and apache-ant-1.9.0 from one machine to another machine into C:\buildscript_required_files_v2 folder.

Now i have a window batch file that gets into the path where build.xml resides and run ant

cd VersionBuild
C:\buildscript_required_files_v2\apache-ant-1.9.0\bin\ant

build.xml complies the Java class and creates a Jar file.

<project name="VersionBuild"  default="clean" basedir=".">
    <description>
        simple example build file
    </description>

  <!-- set global properties for this build -->


  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->


 </target>

 <target name="CompilingBuildversion" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <C:\buildscript_required_files_v2\java\jdk64\bin\javac.exe srcdir="." destdir="."/>
  </target>

  <target name="Creating jar" depends="CompilingBuildversion">
      <jar jarfile="VersionBuild.jar"  basedir="."/>
 </target>    


 <target name="clean" depends="Creating jar">

 </target>

</project>

when I am running that bach file, I am getting the following exception -

Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre6\li
b\tools.jar

Can I run ant build-script without installing Java and setting any environment variable? Can i specify JAVA_HOME for ant locally into the build.xml so ant can take refrence from C:\buildscript_required_files_v2 folder?


Solution

  • You problem is not concerned with *_HOME variables, but I first answer you question. Yes you can.

    Just configure your PATH variable to (1) dir where java.exe resides (2) where ant.bat resides. In case when *_HOME defined the path can be written:

    PATH=...;%JAVA_HOME%\bin;%ANT_HOME%
    

    Since you have no such variables you need declare:

    PATH=...;C:\Program Files\Java\bin;c:\ant\bin
    

    But in real you problem that you try use JRE while ant needs JDK. Just download from oracle site. tools.jar is part of JDK but not JRE.

    UPDATE: You can write you own bat file that lets Windows know where to locate .exe and .bat files. Just create in notepad text file named my-ant.bat And place following there:

    set JAVA_HOME=C:\buildscript_required_files_v2\java\jdk64
    set PATH=%PATH%;%JAVA_HOME%\bin;C:\buildscript_required_files_v2\apache-ant-1.9.0\bin
    rem ** Now we invoke ant **
    ant 
    

    Obviously you would like manipulate with command line arguments. That is why instead of last ant line use following:

    set my_ant_start=
    :setupArgs
    if ""%1""=="""" goto doneStart
    set my_ant_start=%my_ant_start% %1
    shift
    goto setupArgs
    :doneStart
    rem ** Now we invoke ant **
    ant %my_ant_start%