ant - 如何在命令行中执行 Ant build

我有一个 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"/>

...

最佳答案

转到 Ant 网站和 download .这样,您就有了 Eclipse 之外的 Ant 副本。我建议把它放在 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 如何决定要执行的任务。

    看看是否有帮助。

    https://stackoverflow.com/questions/19038279/

    相关文章:

    eclipse - 用ant构建eclipse项目

    powershell - 如何显示 Visual Studio Online 构建任务 Write-

    java - Android Gradle 将外部库和嵌套的外部库添加到项目中

    jenkins - 有没有办法在声明性 Jenkins 管道中运行预结帐步骤?

    build - 有没有基于类似inotify机制的构建工具

    unit-testing - 每晚构建与持续集成 : Long-Running Automated

    javascript - 如何在 Grunt 目标之间共享文件?

    linux - Make的输出,括号内的数字

    node.js - Nodejs npm 步骤在 TeamCity 的每个构建中下载包

    java - Gradle:如何使用并排项目配置多项目设置