1.第一个Ant例子(运行版本为1.8.4)
1) 一个build.xml文件有一个project标签,project的属性default指定默认执行的target。
2) project标签中可以有一个description标签对project进行描述。
3) project标签可以有多个target标签,target标签属性name指定target的名称,description对target进行描述,depends属性指定target所依赖的target。
4) 每个target中可以有多个要执行的命令。
<?xml version="1.0" encoding="UTF-8"?> <project name="firstbuild" default="execute"> <description>第一个简单的Build文件</description> <target name="init" description="创建项目文件夹"> <mkdir dir="build/classes"/> <mkdir dir="dist"/> </target> <target name="compile" depends="init" description="编译Java源文件"> <javac includeAntRuntime="false" srcdir="src" destdir="build/classes"/> <echo level="info">compilation complete!</echo> </target> <target name="archive" depends="compile" description="创建Jar文件"> <jar destfile="dist/project.jar" basedir="build/classes" /> </target> <target name="clean" description="清除项目文件夹"> <delete dir="build"/> <delete dir="dist"/> </target> <target name="execute" depends="compile" description="运行项目"> <java classname="xuj.ant.Main" classpath="build/classes"> <arg value="c"/> <arg value="d"/> <arg value="测试"/> <arg file="."/> </java> </target> </project>
2. 部分标签属性解释
echo标签属性level:
level属性的值可以是:error,warning,info,verbose,debug.默认为 warning
javac标签属性 includeAntRuntime :
includeAntRuntime:是否包含Ant中的jar。默认为True。
3. 控制输出信息
>ant(默认输出日志信息,还装饰内容)
>ant
Buildfile: E:\antspace\firstbuild\build.xml
init:
[mkdir] Created dir: E:\antspace\firstbuild\build\classes
[mkdir] Created dir: E:\antspace\firstbuild\dist
compile:
[javac] Compiling 1 source file to E:\antspace\firstbuild\build\classes
[echo] compilation complete!
execute:
[java] c
[java] d
[java] 测试
[java] E:\antspace\firstbuild
[java] 完成!
BUILD SUCCESSFUL
Total time: 1 second
>ant -debug/-d(包含-verbose模式的输出,还包含大量底层的信息,Ant开发人员有兴趣)
>ant -debug
Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Trying the default build file: build.xml
Buildfile: E:\antspace\firstbuild\build.xml
Adding reference: ant.PropertyHelper
Detected Java version: 1.6 in: D:\dev\java\jdk16027\jre
Detected OS: Windows 7
Adding reference: ant.ComponentHelper
Setting ro project property: ant.file -> E:\antspace\firstbuild\build.xml
Setting ro project property: ant.file.type -> file
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile E:\antspace\firstbuild\build.xml with URI = file:/E:/antspace/
firstbuild/build.xml
Setting ro project property: ant.project.name -> firstbuild
Adding reference: firstbuild
Setting ro project property: ant.project.default-target -> execute
Setting ro project property: ant.file.firstbuild -> E:\antspace\firstbuild\build
.xml
Setting ro project property: ant.file.type.firstbuild -> file
Project base dir set to: E:\antspace\firstbuild
+Target:
+Target: init
+Target: compile
+Target: archive
+Target: clean
+Target: execute
Adding reference: ant.LocalProperties
parsing buildfile jar:file:/D:/dev/ant/ant1.8.4/lib/ant.jar!/org/apache/tools/an
t/antlib.xml with URI = jar:file:/D:/dev/ant/ant1.8.4/lib/ant.jar!/org/apache/to
ols/ant/antlib.xml from a zip file
Setting ro project property: ant.project.invoked-targets -> execute
Attempting to create object of type org.apache.tools.ant.helper.DefaultExecutor
Adding reference: ant.executor
Build sequence for target(s) `execute' is [init, compile, execute]
Complete build sequence is [init, compile, execute, clean, archive, ]
init:
[mkdir] Created dir: E:\antspace\firstbuild\build\classes
[mkdir] Created dir: E:\antspace\firstbuild\dist
compile:
fileset: Setup scanner in dir E:\antspace\firstbuild\src with patternSet{ includ
es: [] excludes: [] }
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.class skipped - don't know h
ow to handle it
[javac] xuj\ant\Main.java added as xuj\ant\Main.class doesn't exist.
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.java.bak skipped - don't kno
w how to handle it
[javac] Compiling 1 source file to E:\antspace\firstbuild\build\classes
[javac] Using modern compiler
[javac] Compilation arguments:
[javac] '-d'
[javac] 'E:\antspace\firstbuild\build\classes'
[javac] '-classpath'
[javac] 'E:\antspace\firstbuild\build\classes'
[javac] '-sourcepath'
[javac] 'E:\antspace\firstbuild\src'
[javac] '-g:none'
[javac]
[javac] The ' characters around the executable and arguments are
[javac] not part of the command.
[javac] File to be compiled:
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.java
[echo] compilation complete!
execute:
[java] running xuj.ant.Main with default permissions (exit forbidden)
[java] Running in same VM Executing 'xuj.ant.Main' with arguments:
[java] 'c'
[java] 'd'
[java] '测试'
[java] 'E:\antspace\firstbuild'
[java]
[java] The ' characters around the executable and arguments are
[java] not part of the command.
force loading xuj.ant.Main
Finding class xuj.ant.Main
Loaded from E:\antspace\firstbuild\build\classes xuj/ant/Main.class
Class java.lang.Object loaded from parent loader (parentFirst)
Class java.lang.String loaded from parent loader (parentFirst)
Class java.lang.System loaded from parent loader (parentFirst)
Class java.io.PrintStream loaded from parent loader (parentFirst)
[java] c
[java] d
[java] 测试
[java] E:\antspace\firstbuild
[java] 完成!
BUILD SUCCESSFUL
Total time: 0 seconds
>ant -verbose/-v(打印冗长的输出,有利于更好地调试)
ant -v
Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Trying the default build file: build.xml
Buildfile: E:\antspace\firstbuild\build.xml
Detected Java version: 1.6 in: D:\dev\java\jdk16027\jre
Detected OS: Windows 7
parsing buildfile E:\antspace\firstbuild\build.xml with URI = file:/E:/antspace/
firstbuild/build.xml
Project base dir set to: E:\antspace\firstbuild
parsing buildfile jar:file:/D:/dev/ant/ant1.8.4/lib/ant.jar!/org/apache/tools/an
t/antlib.xml with URI = jar:file:/D:/dev/ant/ant1.8.4/lib/ant.jar!/org/apache/to
ols/ant/antlib.xml from a zip file
Build sequence for target(s) `execute' is [init, compile, execute]
Complete build sequence is [init, compile, execute, clean, archive, ]
init:
[mkdir] Created dir: E:\antspace\firstbuild\build\classes
[mkdir] Created dir: E:\antspace\firstbuild\dist
compile:
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.class skipped - don't know h
ow to handle it
[javac] xuj\ant\Main.java added as xuj\ant\Main.class doesn't exist.
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.java.bak skipped - don't kno
w how to handle it
[javac] Compiling 1 source file to E:\antspace\firstbuild\build\classes
[javac] Using modern compiler
[javac] Compilation arguments:
[javac] '-d'
[javac] 'E:\antspace\firstbuild\build\classes'
[javac] '-classpath'
[javac] 'E:\antspace\firstbuild\build\classes'
[javac] '-sourcepath'
[javac] 'E:\antspace\firstbuild\src'
[javac] '-g:none'
[javac]
[javac] The ' characters around the executable and arguments are
[javac] not part of the command.
[javac] File to be compiled:
[javac] E:\antspace\firstbuild\src\xuj\ant\Main.java
[echo] compilation complete!
execute:
[java] running xuj.ant.Main with default permissions (exit forbidden)
[java] Running in same VM Executing 'xuj.ant.Main' with arguments:
[java] 'c'
[java] 'd'
[java] '测试'
[java] 'E:\antspace\firstbuild'
[java]
[java] The ' characters around the executable and arguments are
[java] not part of the command.
[java] c
[java] d
[java] 测试
[java] E:\antspace\firstbuild
[java] 完成!
BUILD SUCCESSFUL
Total time: 1 second
>ant -emacs(生成日志信息,去掉装饰性的内容)
>ant -emacs
Buildfile: E:\antspace\firstbuild\build.xml
init:
Created dir: E:\antspace\firstbuild\build\classes
Created dir: E:\antspace\firstbuild\dist
compile:
Compiling 1 source file to E:\antspace\firstbuild\build\classes
compilation complete!
execute:
c
d
测试
E:\antspace\firstbuild
完成!
BUILD SUCCESSFUL
Total time: 0 seconds
>ant -quiet/-q(运行安静的构建,只打印错误信息,会打印echo标签中level为warning级别以上的信息)
>ant -quiet
BUILD SUCCESSFUL
Total time: 1 second
3.获取项目信息
>ant -projecthelp/-p(打印关于当前项目的信息)
>ant -projecthelp
Buildfile: E:\antspace\firstbuild\build.xml
第一个简单的Build文件
Main targets:
archive 创建Jar文件
clean 清除项目文件夹
compile 编译Java源文件
execute 运行项目
init 创建项目文件夹
Default target: execute
4.指定要运行的构建文件
>ant -buildfile build.xml
>ant -buildfile build.xml
Buildfile: E:\antspace\firstbuild\build.xml
init:
[mkdir] Created dir: E:\antspace\firstbuild\build\classes
[mkdir] Created dir: E:\antspace\firstbuild\dist
compile:
[javac] Compiling 1 source file to E:\antspace\firstbuild\build\classes
[echo] compilation complete!
execute:
[java] c
[java] d
[java] 测试
[java] E:\antspace\firstbuild
[java] 完成!
BUILD SUCCESSFUL
Total time: 1 second
5.运行指定目标
>ant clean(这里指运行目标名称为clean的目标)
>ant clean
Buildfile: E:\antspace\firstbuild\build.xml
clean:
[delete] Deleting directory E:\antspace\firstbuild\build
[delete] Deleting directory E:\antspace\firstbuild\dist
BUILD SUCCESSFUL
Total time: 0 seconds
6.运行多个目标
>ant clean execute
>ant clean execute
Buildfile: E:\antspace\firstbuild\build.xml
clean:
[delete] Deleting directory E:\antspace\firstbuild\build
[delete] Deleting directory E:\antspace\firstbuild\dist
init:
[mkdir] Created dir: E:\antspace\firstbuild\build\classes
[mkdir] Created dir: E:\antspace\firstbuild\dist
compile:
[javac] Compiling 1 source file to E:\antspace\firstbuild\build\classes
[echo] compilation complete!
execute:
[java] c
[java] d
[java] 测试
[java] E:\antspace\firstbuild
[java] 完成!
BUILD SUCCESSFUL
Total time: 1 second
7.-keep-going/-k(当命令行中的一个目标失败后,继续运行其他目标)
>ant execute clean
Buildfile: E:\antspace\firstbuild\build.xml
init:
compile:
BUILD FAILED
E:\antspace\firstbuild\build.xml:12: Problem: failed to create task or type java
ac
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Total time: 0 seconds
>ant -k execute clean
Buildfile: E:\antspace\firstbuild\build.xml
init:
compile:
Target 'compile' failed with message 'Problem: failed to create task or type jav
aac
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
'.
Cannot execute 'execute' - 'compile' failed or was not executed.
clean:
[delete] Deleting directory E:\antspace\firstbuild\build
[delete] Deleting directory E:\antspace\firstbuild\dist
BUILD FAILED
E:\antspace\firstbuild\build.xml:12: Problem: failed to create task or type java
ac
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Total time: 0 seconds