如何在命令行中执行Ant构建

时间:2013-09-26 20:40:06

标签: ant build deferred-execution

我有一个Ant构建文件,我尝试使用以下命令在命令行中执行它:

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

但没有任何反应,结果是:

BUILD SUCCESSFUL
Total time: 0 seconds

我的环境变量是正确的。
问题是什么?这是我的构建文件:

<?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"/>

...

3 个答案:

答案 0 :(得分:20)

转到Ant网站和download。这样,您就拥有了Eclipse的外部的副本。我建议把它放在C:\ant目录下。这样,它在目录名称中没有任何空格。在系统控制面板中,将环境变量ANT_HOME设置为此目录,然后预先挂起到系统PATH变量%ANT_HOME%\bin。这样,您就不必输入整个目录名称。

假设你做了以上事情,试试这个:

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

这将做几件事:

  • 这将消除问题出现在Eclipe的Ant版本中。
  • 输入
  • 更容易
  • 由于您在其所在的目录中执行build.xml,因此您最终无法确定您的Ant版本无法找到特定目录。

-d将打印出大量输出,因此您可能希望捕获它,或将终端缓冲区设置为99999,并先运行cls以清除缓冲区。这样,您将从终端缓冲区中开始捕获所有输出。

让我们看看Ant应该如何执行。您没有指定要执行的任何目标,因此Ant应该采用默认的build目标。这是:

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

build目标本身没有任何作用。但是,取决于在其他两个目标上,因此首先调用它们:

第一个目标是build-subprojects

<target name="build-subprojects"/>

这一点都没有。它甚至没有依赖性。

指定的下一个目标build-project确实有代码:

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

此目标包含任务, 某些相关目标。在build-project执行之前,它将首先运行init目标:

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

此目标创建名为bin的目录,然后将src树下带有后缀*.java的所有文件复制到bin目录。 includeemptydirs表示不会创建没有非java代码的目录。

Ant使用一种方案来完成最少的工作。例如,如果创建了bin目录,则不会执行<mkdir/>任务。此外,如果先前已复制文件,或者src目录树中没有非Java文件,则<copy/>任务将无法运行。但是,init目标仍将执行。

接下来,我们回到之前的build-project目标:

<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>

看看这一行:

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

应该一直执行。您的输出是否打印出来:

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

也许你没有意识到这是来自你的构建。

之后,它运行<javac/>任务。也就是说,如果有任何文件要实际编译。同样,Ant试图避免它不必做的工作。如果先前已编译了所有*.java个文件,则<javac/>任务将无法执行。

而且,这就是构建的结束。你的构建可能没有做任何事情只是因为无事可做。您可以尝试运行clean任务,然后build

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

但是,Ant通常会打印正在执行的目标。你应该看到这个:

init:

build-subprojects:

build-projects:

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

build:

Build Successful

请注意,目标全部打印出来以便执行,并且任务在执行时打印出来。但是,如果没有可编译的内容,或者没有任何内容可以复制,那么您将无法看到正在执行的这些任务。这看起来像你的输出?如果是这样,它可能无处可去。

  • 如果bin目录已存在,<mkdir/>将无法执行。
  • 如果src中没有非Java文件,或者它们已被复制到bin,则<copy/>任务将无法执行。
  • 如果您的src目录中没有Java文件,或者它们已经编译过,那么<java/>任务就不会运行。

如果查看-d调试的输出,您将看到Ant查看任务,然后解释为什么没有执行特定任务。另外,调试选项将解释Ant如何决定要执行的任务。

看看是否有帮助。

答案 1 :(得分:0)

它仍然是实际的吗?

我可以看到你写了<target depends="build-subprojects,build-project" name="build"/>,然后你写了<target name="build-subprojects"/>(它什么也没做)。这可能是一个原因吗? 这<echo message="${ant.project.name}: ${ant.file}"/>是否打印了相应的消息?如果没有那么目标没有运行。 请查看下一个链接http://www.sqaforums.com/showflat.php?Number=623277

答案 2 :(得分:0)

尝试单独运行所有目标以检查所有目标是否正常运行

运行ant目标名称以单独运行目标

e.g。 ant build-project

您指定的默认目标是

  

project basedir =“。” default =“build”name =“iControlSilk4J”

这只会执行build-subprojects,build-project和init