51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 6173|回复: 0
打印 上一主题 下一主题

Ant之HelloWorld

[复制链接]
  • TA的每日心情
    无聊
    2024-3-7 09:16
  • 签到天数: 43 天

    连续签到: 2 天

    [LV.5]测试团长

    跳转到指定楼层
    1#
    发表于 2018-6-15 16:23:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    工作需要需要接触到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 pr
    oject includes an implicit target that contains any and all top-level tasks and/or types. This target will alw
    ays 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 overridde
    n by setting the "basedir" property beforehand. When this is done, it must be omitted in the project ta
    g. If neither the attribute nor the property have been set, the parent directory of the buildfile will be use
    d.        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>



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-19 12:15 , Processed in 0.067219 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表