使用ant编译java项目的简单实例

来源:互联网 发布:人工智能app是什么 编辑:程序博客网 时间:2024/05/17 02:55

  1. 下载apache-ant-xxx.zip
  2. 解压缩
  3. 设置环境变量,指向解压缩的ant的bin目录
  4. 可在命令行里执行ant -version查看ant是否安装完成
  5. 在要编译的项目同级目录新建build.xml配置文件,内容如下所示:
  6. 在命令行里定位到build.xml所在的目录,执行命令ant即可开始编译。

<project name="SMSUtil" default="dist" basedir=".">  <!-- 项目名,default的值是对应下面默认执行的target(任务) -->    <description>        simple example build file    </description>  <!-- set global properties for this build -->  <property name="project" location="SMSUtil"/>  <property name="src" location="${project}/src"/> <!-- 设置变量,指向要编译的java代码的位置 -->  <property name="lib.dir" location="${project}/lib"/> <!-- 设置变量,指向所依赖的jar包所在的位置 -->  <property name="build" location="${project}/build"/> <!-- 设置变量,指向编译后的class文件的位置 -->  <property name="dist"  location="${project}/dist"/> <!-- 设置变量,指向编译后生成jar包的位置 -->  <!-- 设置要依赖的jar包规则 -->  <path id="project.class.path">  <pathelement path="${build}" />  <fileset dir="${lib.dir}">  <include name="**/*.jar" />  </fileset>  </path>    <target name="init">    <!-- Create the time stamp -->    <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="on" classpath="${lib}"><classpath refid="project.class.path" /> <!-- 引入依赖的jar包 --></javac>  </target>  <target name="dist" depends="compile"        description="generate the distribution" >    <!-- Create the distribution directory -->    <mkdir dir="${dist}/lib"/>    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->    <jar jarfile="${dist}/lib/SMSUtil-${DSTAMP}.jar" basedir="${build}"/>  <!-- 配置生成的jar包的路径 -->  </target>  <target name="clean"        description="clean up" >    <!-- Delete the ${build} and ${dist} directory trees -->    <delete dir="${build}"/>    <delete dir="${dist}"/>  </target></project>


微笑

0 0