Ant简介

2018-09-16  本文已影响68人  acc8226

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' 中:

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

关于Classpath一些笔记

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

入门小案例

<?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
base Visual Studio Code

>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支持循环

上一篇下一篇

猜你喜欢

热点阅读