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

  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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值