ant使用备忘

ant是一个脚本构建工具,可能就是持续集成里面所需要的构建工具。

如果使用eclipse,里面会自带有ant工具,不需要再安装了,创建一个build.xml(或者其他的名字都可以),使用ant来运行就可以了

如果使用的命令行来运行ant,需要安装的步骤如下:

(1)下载ant,解压,无需安装过程。

(2)将ant的bin目录加入到系统的环境变量path中。

(3)运行ant,可以查看是否加成功了。

(4)在cmd中,将目录切换到工程所在的build.xml目录下,运行ant(这里默认为build.xml文件,还可以自己制定需要的文件)

关于build.xml文件的目录结构

<project name="**">

<property name="src" value="test" />

<path id="classpath1.path">
<fileset dir="${lib.dir}">

<property name="dest" value="classes" />

<property name="lib.dir" value="lib"/>

<include name="*.jar" />

</fileset>

<pathelement location="${src}" />

<pathelement location="${dest}"/>
</path>

       <!-- 定义一些初始化的操作 !-->

<target name="init">
    <delete verbose="true" includeemptydirs="true">
    <fileset dir="${dest}">
        <include name="**/*"/>
    </fileset>
</delete>
<mkdir dir="${dest}" />
</target>  

<!--编译 !-->

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="classpath1.path"/>
<echo message="${TIME}" />
</target>

<!--接下来有一些你需要做的事情,比如打包部署,或者运行一个程序等 !-->

...

</project>


一些需要记录的东西:

(1)运行一个java程序的定义:

<target name="sendmail" depends="compile">
   <java classname="com.sogou.maps.testApi.Integration" classpathref="classpath1.path">
       <classpath path="${dest}"></classpath>
       <arg value="${basedir}/${TIME}/emailable-report.html"></arg>
   </java>
</target>

如果引用了第三方的包,那么需要在classpathref中定义,否则会提示错误信息。


(2)定义一个时间戳

<tstamp> 
         <format property="TIME" pattern="yyyyMMdd-hhmm" locale="en,UK"/> 
    </tstamp>

只要定义了这个,就定义了三个参数:DSTAMP,TSTAMP,TODAY,在下面的内容中就可以使用${DSTAMP}等了。在上面我们定义了自己的参数TIME,可以使用。这个时间戳在整个build过程中是不变的。


(3)在命令行里运行的参数

命令行选项总结:
ant [options] [target [target2 [target3] ...]]
Options:
-help print this message
-projecthelp print project help information
-version print the version information and exit
-quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-emacs produce logging information without adornments
-logfile file use given file for log output
-logger classname the class that is to perform logging
-listener classname add an instance of class as a project listener
-buildfile file use specified buildfile
-find file search for buildfile towards the root of the filesystem and use the first one found
-Dproperty=value set property to value 


(4)一些ant自带的内置变量

os.name:       操作系统的名称。

basedir: 项目引用的根路径。

ant.home:     Ant工具的根目录位置。

ant.file: 构件文件的绝对路径。

ant.version:   ant的版本信息。

ant.java.version: ant检测到的JVM版本。

ant.project.name: 当前执行的项目名称及信息。

java.version: Java的版本信息。

java.home:    Java的根目录位置。

java.class.path: Java类库的文件的位置。

line.separator:   换行符.


(5)编译的时候如果在windows下面使用命令行来运行ant的话,工程使用UTF-8,编译会有编码错误。需要设定编译的编码

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="classpath1.path">
<compilerarg line="-encoding UTF-8"></compilerarg>
</javac>
<echo message="${TIME}" />
</target>

原文地址:https://www.cnblogs.com/shenguo/p/3958231.html