Ant 简介-上

Apache Ant 是由 Java 语言开发的工具,由 Apache 软件基金会所提供。Apache Ant 的配置文件写成 XML 容易维护和书写,而且结构很清晰。本教程将以简单的方式会向你展示如何利用 Apache ANT 来自动地构建和部署过程。

  1. 下载到某个目录解压到, 例如安装路径C:\L_Executable\apache-ant-1.10.5
  2. 配置环境变量
ANT_HOME C:\L_Executable\apache-ant-1.10.5
Path %ANT_HOME%\bin
  1. 运行ant -version查看是否安装成功Apache Ant(TM) version 1.10.5 compiled on July 10 2018
    <!-- copy -->
    <!-- 拷贝文件 -->
    <copy file=".\build.xml" tofile="newBuild.xml" />   
    <!-- 拷贝文件夹 -->
    <copy todir="build/dest_dir">
        <fileset dir="origin_dir" />
    </copy>
    
    <!-- delete -->
    <!-- 删除单个文件 -->
    <delete file="test.txt" />
    <!-- 删除整个目录 -->
    <delete dir="someDir" />

    <!-- zip -->
    <!-- 将当前目录包含文件压缩 -->
    <zip destfile="project.zip" basedir="."/>

    

插播Java教程

javac的官方说法:

-classpath:
设置用户类路径,它将覆盖CLASSPATH 环境变量中的用户类路径。若既未指定CLASSPATH 又未指定-classpath,则用户类路径由当前目录构成。
-sourcepath:
指定用以查找类或接口定义的源代码路径。与用户类路径一样,源路径项用分号 (;)进行分隔,它们可以是目录、JAR 归档文件或 ZIP 归档文件。如果使用包,那么目录或归档文件中的本地路径名必须反映包名。

注意:通过类路径查找的类,如果找到了其源文件,则可能会自动被重新编译。

-d用于指定.class文件的生成目录, 将目录 src/com/tt下Hello.Java类编译到bin目录下
美中不足的是-d需要指定已经存在的目录,不能自动创建。
javac -sourcepath src -classpath . -d bin src/com/tt/Hello.java
如果没什么其他类的依赖可简写为 javac -d bin src/com/tt/Hello.java

java会基于提供的classpath(缩写成cp)路径去搜索。
java -classpath bin com.tt.Hello

bin/目录中的所有文件归档到 'classes.jar' 中:

  • 方法一: 指定MANIFEST.MF文件的命令: jar vcfm classes.jar MANIFEST.MF -C bin/ .

  • 方法二: 先直接生成
    jar vcf classes.jar -C bin/ .
    再winRAR直接修改MANIFEST.MF


    或者拿出MANIFEST.MF文件后命令jar vufm classes.jar MANIFEST.MF, 这里注意要空两个空行, 如果遇到Duplicate name in Manifest: Created-By.这种语句直接忽略就好, 因为字段有重名而已。

之所以加v是为了生成详细输出, 去掉也没影响

关于Classpath一些笔记

Classpath可以用3种不同的方式设置:

  • 如果没有设置——那么classpath参数就会被忽略,环境变量中的CLASSPATH就会被使用到
  • 如果环境变量CLASSPATH没找到,那么就是默认使用当前目录(”.”)
  • 如果classpath作为命令行参数显示设置了,那么它就是覆盖所有其他的值。 当设置覆盖默认值(当前目录)时,classpath会造成不可预料的结果。 所以要么省略, 要么-cp .;lib/aaa.jar例如为javac -cp .;lib/aaa.jar bbb.java

入门小案例

<?xml version="1.0"?>
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>  

    <!-- 加载配置文件 -->
    <property file="build.properties"/>

    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="build" location="build/classes"/>
    <property name="dist" location="dist"/>

    <target name="init">
        <!-- Create the time stamp -->
        <tstamp>
            <format property="DSTAMP" pattern="yyMMdd" />
        </tstamp>

        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
    </target>

    <target name="compile" depends="init"
        description="compile the source">
        <!-- Compile the Java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>
    </target>

    <target name="dist" depends="compile"
        description="generate the distribution">
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}/lib"/>
        <!-- Put everything in ${build} into the jar file -->
        <jar jarfile="${dist}/lib/${jar.filename}-${DSTAMP}.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-class" value="${jar.manifest.main-class}" />
            </manifest>
        </jar>
    </target>

    <target name="clean" description="clean up">
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
    </target>

</project>

ant命令一览

OS: win10 64bit

>ant -help
ant [options] [target [target2 [target3] ...]]
Options:
  -help, -h              print this message and exit
  -projecthelp, -p       print project help information and exit
  -version               print the version information and exit
  -diagnostics           print information that might be helpful to
                         diagnose or report problems and exit
  -quiet, -q             be extra quiet
  -silent, -S            print nothing but task outputs and build failures
  -verbose, -v           be extra verbose
  -debug, -d             print debugging information
  -emacs, -e             produce logging information without adornments
  -lib <path>            specifies a path to search for jars and classes
  -logfile <file>        use given file for log
    -l     <file>                ''
  -logger <classname>    the class which is to perform logging
  -listener <classname>  add an instance of class as a project listener
  -noinput               do not allow interactive input
  -buildfile <file>      use given buildfile
    -file    <file>              ''
    -f       <file>              ''
  -D<property>=<value>   use value for given property
  -keep-going, -k        execute all targets that do not depend
                         on failed target(s)
  -propertyfile <name>   load all properties from file with -D
                         properties taking precedence
  -inputhandler <class>  the class which will handle input requests
  -find <file>           (s)earch for buildfile towards the root of
    -s  <file>           the filesystem and use it
  -nice  number          A niceness value for the main thread:                         1 (lowest) to 10 (highest); 5 is the default
  -nouserlib             Run ant without using the jar files from                         ${user.home}/.ant/lib
  -noclasspath           Run ant without using CLASSPATH
  -autoproxy             Java1.5+: use the OS proxy settings
  -main <class>          override Ant's normal entry point

ANT Contrib

让 ant 支持循环

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容