文章目录
- 1.下载ant
- 2. 解压ant
- 3. 配置an环境变量
- 4. 验证
- 二、编译项目
- 2.1. 新建一个build.xml
- 2.2. 编译项目测试
1.下载ant
官网链接:
https://ant.apache.org/srcdownload.cgi
2. 解压ant
3. 配置an环境变量
4. 验证
ant -v
二、编译项目
2.1. 新建一个build.xml
添加内容如下:
声明:只需要修改项目名和jar的路径即可
建议复制,然后把项目名称统一替换,因为有多处用到项目名,最后,修改jar的路径即可
<project name="lis" default="dist" basedir="."> <!-- 项目名,default的值是对应下面默认执行的target(任务) -->
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="project" location="lis"/>
<property name="src" location="${project}/java/src"/> <!-- 设置变量,指向要编译的java代码的位置 -->
<property name="lib.dir" location="${project}/ui/WEB-INF/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 encoding="GBK" 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/lis-${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>
2.2. 编译项目测试
把build.xml文件和项目放到一个文件夹下面,命令窗口:运行ant
即可执行编译