我有一个 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
build.xml
在它存在的目录中,您最终不会遇到 Ant 构建无法定位特定目录的可能性。 -d
将打印出大量输出,因此您可能想要捕获它,或者将终端缓冲区设置为类似 99999
的内容。 ,然后运行 cls
首先清除缓冲区。这样,您将在终端缓冲区中从头开始捕获所有输出。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 代码的目录。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
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/