Ant:Ant 入门

背景

自从有了 Maven 以后,Ant 视乎就不流行了,不过 Ant 还是有其应用场景的,Ant 的思想比较简洁,如下:

  1. 一个 project 包含多个 target(类似成员方法)。
  2. 一个 target 包含多个 task(类似语句)。
  3. project 中可以声明 property(类似成员变量)。
  4. project 中可以声明 path (特殊的成员变量)。
  5. target 可以声明其 depends。

示例

build.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <project name="02-ant" basedir="." default="help">
 3     <property name="src" value="src" />
 4     <property name="classes" value="bin" />
 5     <property name="output" value="output" />
 6 
 7     <path id="classpath">
 8         <pathelement path="${classes}" />
 9     </path>
10 
11     <target name="help" description="帮助">
12         <echo>help - 帮助</echo>
13         <echo>compile - 编译</echo>
14         <echo>run - 运行</echo>
15         <echo>build - 打包</echo>
16         <echo>clean - 清理</echo>
17     </target>
18 
19     <target name="compile" description="编译">
20         <delete dir="${classes}" />
21         <mkdir dir="${classes}" />
22         <javac srcdir="${src}" destdir="${classes}" debug="true" includeantruntime="false">
23             <classpath refid="classpath" />
24         </javac>
25     </target>
26 
27     <target name="run" description="运行" depends="compile">
28         <java classname="Program" fork="yes">
29             <classpath refid="classpath" />
30             <arg line="段光伟" />
31         </java>
32     </target>
33 
34     <target name="build" description="打包" depends="compile">
35         <delete dir="${output}" />
36         <mkdir dir="${output}" />
37         <jar destfile="${output}/app.jar" basedir="${classes}" includes="**/*.class">
38             <manifest>
39                 <attribute name="Main-Class" value="Program"/>
40                 <attribute name="Class-Path" value="${classes}"/>
41                 <attribute name="classpath" value="${classes}"/>
42             </manifest>
43         </jar>
44     </target>
45     
46     <target name="clean" description="清理">
47         <delete dir="${classes}" />
48         <delete dir="${output}" />
49     </target>
50 
51 </project>

目录结构

运行结果

备注

很多非 Java 环境都使用了 Ant,如:ExtJs 提供的工具。

 

posted on 2013-10-16 21:39  幸福框架  阅读(2959)  评论(0编辑  收藏  举报

导航

我要啦免费统计