现在的位置: 首页 > 综合 > 正文

Ant之HelloWorld

2013年08月23日 ⁄ 综合 ⁄ 共 2898字 ⁄ 字号 评论关闭

 工作需要需要接触到Ant Maven这类东西,虽然我很多的时候不用写代码,但为了更好的理解整个内容。我决定重新学习Ant.几年前做开发的时候也曾用过,不过那个时候我用的最多的就是那个build的命令。而现在我需要搞清楚这个build是怎么建立以来,依赖关系是什么样的,以及如何向MAVEN迁移。所以从头开始学吧,毕竟Ant和Maven我都还不咋的懂哎。

 

先写了个hello world,建一般普通的Java Project既可。

package example;

public class HelloWorld {
  
    public static void main(String[] args) {
        System.out.println("just for ant test");
    }

}

 

然后在该project下建立个build.xml文件。

 

<?xml version="1.0"?>
<project name="main" default="all" basedir=".">
   <target name="all" depends="init,compile,jar" >
   </target>
 
   <target name="init">
       <property name="dirs.base" value="${basedir}"/>
       <property name="classdir" value="${dirs.base}/classes"/>
       <property name="src" value="${dirs.base}/src"/>
       <property name="lib" value="${dirs.base}/lib"/>
       <property name="outputjar" value="${dirs.base}/lib/HelloAntWorld.jar"/>
       <mkdir dir="${classdir}"/>
       <mkdir dir="${lib}"/>
   </target>
 
    <target name="jar" depends="init">
           <jar jarfile="${outputjar}" >
                  <fileset dir="${classdir}" />
           </jar>
       </target>
     
  
   <target name="compile" depends="init">
       <javac debug="true" srcdir="${src}"
          destdir="${classdir}" />
   </target>
 
   <target name="clean" depends="init">
       <delete dir="${classdir}"/>
   </target>
</project>

 

 

 然后就可以利用Ant编译了。当然前提条件你需要安装Ant和Java。

D:/HelloAntWorld>ant all
Buildfile: D:/HelloAntWorld/build.xml

init:

compile:
    [javac] D:/HelloAntWorld/build.xml:26: warning: 'includeantruntime' was not
set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to D:/HelloAntWorld/classes

jar:
      [jar] Building jar: D:/HelloAntWorld/lib/HelloAntWorld.jar

all:

BUILD SUCCESSFUL
Total time: 0 seconds

 

就这样一个简单的Ant 入门示例就OK了。

 

话说我这个人现在比较喜欢在网上找资料看,目前英文文档对我来说基本没什么问题,所以尽量找英文文档,一则看得会比中文快,而且比较能够了解作者的意思。在现在的公司工作的最大的好处是我每天除了看到英文还是英文,连OS也是英文的。really a good chance!

 

我以下打算看的文档:

http://ant.apache.org/manual/index.html

 

Ant Build 文件都是XML形式的。每一个build 文件包含一个project 和至少一个target tag.

每一个project定义了一个或者多个target.

 

Attribute Description Required
name the name of the project. No
default the default target to use when no target is supplied. No; however, since Ant 1.6.0
,
every project includes an implicit target that contains any and
all top-level tasks and/or types. This target will always be
executed as part of the project's initialization, even when Ant is
run with the -projecthelp
option.
basedir the base directory from which all path calculations are
done. This attribute might be overridden by setting
the "basedir"
property beforehand. When this is done, it must be omitted in the
project tag. If neither the attribute nor the property have
been set, the parent directory of the buildfile will be used.
No

 

Target: A target is a set of tasks you want to be executed.
即命令的集合。
可以指定哪个target先执行,如果未指定,project的default被执行。

 

A task is a piece of code that can be executed.

Task可以有多个属性。属性的值可以直接refer到一个properity

taskID是task唯一的标识符

 

关于properity:

有名字和值,名字是大小写敏感的,其值可以被task的属性使用,使用的时候用${}进行引用。

比如     <target name="jar" depends="init">
           <jar jarfile="${outputjar}" >
                  <fileset dir="${classdir}
" />
           </jar>
       </target>

 

 

 

抱歉!评论已关闭.