How to execute Ant build in command line

159,432

Go to the Ant website and download. This way, you have a copy of Ant outside of Eclipse. I recommend to put it under the C:\ant directory. This way, it doesn't have any spaces in the directory names. In your System Control Panel, set the Environment Variable ANT_HOME to this directory, then pre-pend to the System PATHvariable, %ANT_HOME%\bin. This way, you don't have to put in the whole directory name.

Assuming you did the above, try this:

C:\> cd \Silk4J\Automation\iControlSilk4J
C:\Silk4J\Automation\iControlSilk4J> ant -d build

This will do several things:

  • It will eliminate the possibility that the problem is with Eclipe's version of Ant.
  • It is way easier to type
  • Since you're executing the build.xml in the directory where it exists, you don't end up with the possibility that your Ant build can't locate a particular directory.

The -d will print out a lot of output, so you might want to capture it, or set your terminal buffer to something like 99999, and run cls first to clear out the buffer. This way, you'll capture all of the output from the beginning in the terminal buffer.

Let's see how Ant should be executing. You didn't specify any targets to execute, so Ant should be taking the default build target. Here it is:

<target depends="build-subprojects,build-project" name="build"/>

The build target does nothing itself. However, it depends upon two other targets, so these will be called first:

The first target is build-subprojects:

<target name="build-subprojects"/>

This does nothing at all. It doesn't even have a dependency.

The next target specified is build-project does have code:

<target depends="init" name="build-project">

This target does contain tasks, and some dependent targets. Before build-project executes, it will first run the init target:

<target name="init">
    <mkdir dir="bin"/>
    <copy includeemptydirs="false" todir="bin">
        <fileset dir="src">
            <exclude name="**/*.java"/>
        </fileset>
    </copy>
</target>

This target creates a directory called bin, then copies all files under the src tree with the suffix *.java over to the bin directory. The includeemptydirs mean that directories without non-java code will not be created.

Ant uses a scheme to do minimal work. For example, if the bin directory is created, the <mkdir/> task is not executed. Also, if a file was previously copied, or there are no non-Java files in your src directory tree, the <copy/> task won't run. However, the init target will still be executed.

Next, we go back to our previous build-project target:

<target depends="init" name="build-project">
    <echo message="${ant.project.name}: ${ant.file}"/>
    <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
        <src path="src"/>
        <classpath refid="iControlSilk4J.classpath"/>
    </javac>
</target>

Look at this line:

<echo message="${ant.project.name}: ${ant.file}"/>

That should have always executed. Did your output print:

[echo] iControlSilk4J: C:\Silk4J\Automation\iControlSilk4J\build.xml

Maybe you didn't realize that was from your build.

After that, it runs the <javac/> task. That is, if there's any files to actually compile. Again, Ant tries to avoid work it doesn't have to do. If all of the *.java files have previously been compiled, the <javac/> task won't execute.

And, that's the end of the build. Your build might not have done anything simply because there was nothing to do. You can try running the clean task, and then build:

C:\Silk4J\Automation\iControlSilk4J> ant -d clean build

However, Ant usually prints the target being executed. You should have seen this:

init:

build-subprojects:

build-projects:

    [echo] iControlSilk4J: C:\Silk4J\Automation\iControlSilk4J\build.xml

build:

Build Successful

Note that the targets are all printed out in order they're executed, and the tasks are printed out as they are executed. However, if there's nothing to compile, or nothing to copy, then you won't see these tasks being executed. Does this look like your output? If so, it could be there's nothing to do.

  • If the bin directory already exists, <mkdir/> isn't going to execute.
  • If there are no non-Java files in src, or they have already been copied into bin, the <copy/> task won't execute.
  • If there are no Java file in your src directory, or they have already been compiled, the <java/> task won't run.

If you look at the output from the -d debug, you'll see Ant looking at a task, then explaining why a particular task wasn't executed. Plus, the debug option will explain how Ant decides what tasks to execute.

See if that helps.

Share:
159,432
Guylene Emond
Author by

Guylene Emond

Updated on December 01, 2020

Comments

  • Guylene Emond
    Guylene Emond over 3 years

    I have an Ant build file, and I try to execute it in the command line with the following command:

    $ C:\Program Files (x86)\.....>ant -f C:\Silk4J\Automation\iControlSilk4J\build.xml
    

    But nothing happens, and the result is:

    BUILD SUCCESSFUL
    Total time: 0 seconds
    

    My environment variable is correct.
    What is the problem? Here is my build file:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- WARNING: Eclipse auto-generated file.
                  Any modifications will be overwritten.
                  To include a user specific buildfile here, simply create one in the same
                  directory with the processing instruction <?eclipse.ant.import?>
                  as the first entry and export the buildfile again. -->
    <project basedir="." default="build" name="iControlSilk4J">
        <property environment="env"/>
        <property name="ECLIPSE_HOME" value="../../../Program Files (x86)/Silk/SilkTest/eclipse"/>
        <property name="junit.output.dir" value="junit"/>
        <property name="debuglevel" value="source,lines,vars"/>
        <property name="target" value="1.6"/>
        <property name="source" value="1.6"/>
        <path id="Silk Test JTF 13.5.0 Library.libraryclasspath">
            <pathelement location="../../../Program Files (x86)/Silk/SilkTest/ng/JTF/silktest-jtf-nodeps.jar"/>
        </path>
        <path id="JUnit 4.libraryclasspath">
            <pathelement location="${ECLIPSE_HOME}/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar"/>
            <pathelement location="${ECLIPSE_HOME}/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar"/>
        </path>
        <path id="iControlSilk4J.classpath">
            <pathelement location="bin"/>
            <pathelement location="lib/apache-log4j.jar"/>
            <pathelement location="lib/commons-io-2.4.jar"/>
            <pathelement location="lib/commons-lang3-3.1.jar"/>
            <pathelement location="lib/junit.jar"/>
            <pathelement location="lib/org.hamcrest.core_1.1.0.v20090501071000.jar"/>
            <pathelement location="lib/silktest-jtf-nodeps.jar"/>
            <path refid="Silk Test JTF 13.5.0 Library.libraryclasspath"/>
            <path refid="JUnit 4.libraryclasspath"/>
            <pathelement location="../../../Users/Admin/Desktop/java-mail-1.4.4.jar"/>
            <pathelement location="../../../Users/Admin/Desktop/javax.activation.jar"/>
            <pathelement location="lib/joda-time-2.3.jar"/>
        </path>
        <target name="init">
            <mkdir dir="bin"/>
            <copy includeemptydirs="false" todir="bin">
                <fileset dir="src">
                    <exclude name="**/*.java"/>
                </fileset>
            </copy>
        </target>
        <target name="clean">
            <delete dir="bin"/>
        </target>
        <target depends="clean" name="cleanall"/>
        <target depends="build-subprojects,build-project" name="build"/>
        <target name="build-subprojects"/>
        <target depends="init" name="build-project">
            <echo message="${ant.project.name}: ${ant.file}"/>
            <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
                <src path="src"/>
                <classpath refid="iControlSilk4J.classpath"/>
            </javac>
        </target>
        <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
        <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
            <copy todir="${ant.library.dir}">
                <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
            </copy>
            <unzip dest="${ant.library.dir}">
                <patternset includes="jdtCompilerAdapter.jar"/>
                <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
            </unzip>
        </target>
        <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
            <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
            <antcall target="build"/>
    
    ...
    
  • Guylene Emond
    Guylene Emond over 10 years
    Unfortunately, it's actual ! I know I'm near of the solution, but still... I look in SQA Fotums and I try what it's propose but the build failed !
  • Guylene Emond
    Guylene Emond over 10 years
    Here is the results : BUILD FAILED Target "build.xml" does not exist in the project "iControlSilk4J". at org.apache.tools.ant.Project.tsort(Project.java:1912) at org.apache.tools.ant.Project.topoSort(Project.java:1820) at org.apache.tools.ant.Project.topoSort(Project.java:1783)
  • Guylene Emond
    Guylene Emond over 10 years
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368‌​) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(D‌​efaultExecutor.java:‌​41)
  • Guylene Emond
    Guylene Emond over 10 years
    at org.apache.tools.ant.Project.executeTargets(Project.java:125‌​1) at org.apache.tools.ant.Main.runBuild(Main.java:809) at org.apache.tools.ant.Main.startAnt(Main.java:217) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109) Total time: 0 seconds
  • Guylene Emond
    Guylene Emond over 10 years
    like... project doesn't exists ?!? <project basedir="." default="build" name="iControlSilk4J"> Why ?!?
  • olyv
    olyv over 10 years
    "Target "build.xml" does not exist in the project "iControlSilk4J"" phrase only means that you run Ant typing "ant build.xml" and Ant can't find target named build.xml. You should run Ant just with command "ant". If you build file is named "build.xml" then just run Ant.
  • Guylene Emond
    Guylene Emond over 10 years
    If I just type ant, the results is :Unable to locate tools.jar. Expected to find it in C:\Program Files (x86)\Java\jre1.5.0_22\lib\tools.jar Buildfile: C:\Silk4J\Automation\iControlSilk4J\build.xml build: [junit] Running com.miranda.icontrol.silk4j.installation.AdministrationCtrl [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] Test com.miranda.icontrol.silk4j.installation.AdministrationCtrl FAILED BUILD SUCCESSFUL Total time: 0 seconds build: [junit] Running com.miranda.icontrol.silk4j.installation.AdministrationCtrl
  • Guylene Emond
    Guylene Emond over 10 years
    Time: 0 seconds Why is it failed ? I try ant -verbose and it gives me this error : I try ant -verbose and it gives me this error : <error message="Bad version number in .class file" number in .class file at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassnknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.acces <error message="Bad version number in .class file"
  • Guylene Emond
    Guylene Emond over 10 years
    access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source)</error>
  • Guylene Emond
    Guylene Emond over 10 years
    I try ant -debug and I get this error : [junit] Couldn't find junit/framework/TestCase.class What does it means ? Maybe it's a missing piece ?!? :-(
  • Guylene Emond
    Guylene Emond over 10 years
    .....and at the end : The ' characters around the executable and arguments are not part of the command. [junit] Running com.miranda.icontrol.silk4j.installation.AdministrationCtrl [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] Test com.miranda.icontrol.silk4j.installation.AdministrationCtrl FAILED
  • Guylene Emond
    Guylene Emond over 10 years
    I run ant -verbose and I get this : [junit] 'crashfile=C:\Silk4J\Automation\iControlSilk4J\junitvmwatche‌​r2522012376646798201‌​.properties' [junit] 'propsfile=C:\Silk4J\Automation\iControlSilk4J\junit53676398‌​9337447728.propertie‌​s' [junit] [junit] The ' characters around the executable and arguments are [junit] not part of the command. [junit] Running com.miranda.icontrol.silk4j.installation.AdministrationCtrl [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] Test com.miranda.icontrol.silk4j.installation.AdministrationCtrl FAILED
  • Guylene Emond
    Guylene Emond over 10 years
    why it gives crash at junitwatcher2522... ?!? Is it the cause ?!?
  • Guylene Emond
    Guylene Emond over 10 years
    I found there's a version problem. I resolve this one but now, I have this error : <error message="org/apache/log4j/Logger" type="java.lang.NoClassDefFoundError">java.lang.NoClassDefFo‌​undError: org/apache/log4j/Logger at com.miranda.icontrol.silk4j.installation.AdministrationCtrl.‌​<clinit>(Administrat‌​ionCtrl.java:41) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger at java.net.URLClassLoader$1.run(Unknown Source) at
  • Guylene Emond
    Guylene Emond over 10 years
    ....java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)</error> What does it mean ?