完整的build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="zip">
<!--变量-->
<property name="src.dir" location="src"></property>
<property name="test.src.dir" location="${src.dir}/com/test"></property>
<property name="lib.dir" location="lib"></property>
<property name="build.dir" location="build"></property>
<property name="build.src.dir" location="${build.dir}/src"></property>
<property name="build.classes.dir" location="${build.dir}/classes"></property>
<property name="build.dist.dir" location="${build.dir}/dist"></property>
<property name="build.doc.dir" location="${build.dir}/doc"></property>
<property name="build.test.dir" location="${build.dir}/test"></property>
<property name="build.test.classes.dir" location="${build.test.dir}/classes"></property>
<property name="build.test.report.dir" location="${build.test.dir}/report"></property>
<property name="project.name" value="UserManager"></property>
<property name="version" value="1.0.0"></property>
<property name="build.project.dir" location="${build.dir}/project"></property>
<!--源代码编译环境依赖-->
<path id="compile.source.path">
<fileset dir="${lib.dir}" includes="*.jar"> </fileset>
</path>
<!--测试代码编译环境依赖-->
<path id="compile.test.path">
<path refid="compile.source.path"/>
<pathelement location="${build.classes.dir}"/>
</path>
<!--测试代码运行环境依赖-->
<path id="run.test.path">
<path refid="compile.test.path"/>
<pathelement location="${build.test.classes.dir}"/>
</path>
<!--清理-->
<target name="clean">
<delete dir="build"></delete>
</target>
<!--初始化-->
<target name="init" depends="clean">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src.dir}"/>
<mkdir dir="${build.classes.dir}"/>
<mkdir dir="${build.dist.dir}"/>
<mkdir dir="${build.doc.dir}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes.dir}"/>
<mkdir dir="${build.test.report.dir}"/>
</target>
<!--编译源代码-->
<!--excludes 排除以Test结尾的java-->
<target name="compile-source" depends="init">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}"
classpathref="compile.source.path"
excludes="**/*Test.java" encoding="GBK" >
</javac>
</target>
<!--编译测试代码-->
<target name="compile-test" depends="compile-source">
<javac srcdir="${test.src.dir}" destdir="${build.test.classes.dir}"
classpathref="compile.test.path" encoding="GBK"></javac>
</target>
<!--运行测试代码-->
<target name="run_test" depends="compile-test,copyResource">
<junit printsummary="false">
<classpath refid="run.test.path"></classpath>
<batchtest todir="${build.test.report.dir}">
<fileset dir="${build.test.classes.dir}" includes="**/*.class"></fileset>
</batchtest>
<formatter type="xml"/>
</junit>
<junitreport todir="${build.test.report.dir}" >
<fileset dir="${build.test.report.dir}" includes="TEST-*.xml"></fileset>
<report format="frames" todir="${build.test.report.dir}/html"/>
</junitreport>
</target>
<!--doc文档-->
<target name="doc" depends="run_test" >
<javadoc package="com.*" private="true"
destdir="${build.doc.dir}/api" sourcepath="${src.dir}" classpath="${build.classes.dir}"
charset="GBK" docencoding="GBK" encoding="GBK"
>
</javadoc>
</target>
<!--项目打包-->
<target name="zip" depends="doc,copySourceCode,jar">
<zip destfile="${build.project.dir}/${project.name}-${version}.zip">
<zipfileset dir="${build.doc.dir}" includes="**/*.*" prefix="doc"></zipfileset>
<zipfileset dir="${build.dist.dir}" includes="*.jar" prefix="dist">
</zipfileset>
<zipfileset dir="${build.src.dir}" includes="**/*.*" prefix="src/main"></zipfileset>
<zipfileset dir="${test.src.dir}" includes="**/*.*" prefix="src/test/com/test"></zipfileset>
<zipfileset dir="${lib.dir}" includes="**/*.jar" prefix="lib"></zipfileset>
<zipfileset dir="." includes="*.xml" ></zipfileset>
<zipfileset dir="." includes="*.properties" ></zipfileset>
</zip>
</target>
<!--发布到ftp-->
<target name="ftp" depends="zip">
<ftp password="123" server="localhost" userid="wmk" action="mkdir" remotedir="test">
</ftp>
<ftp password="123" server="localhost" userid="wmk" action="put" remotedir="test">
<fileset dir="${build.project.dir}" includes="*.*"></fileset>
</ftp>
</target>
<!--复制配置文件-->
<target name="copyResource">
<copy todir="${build.classes.dir}" >
<fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java"></fileset>
</copy>
</target>
<!--复制源码-->
<target name="copySourceCode" >
<copy todir="${build.src.dir}">
<fileset dir="${src.dir}" includes="**/*.*"></fileset>
</copy>
</target>
<!-- 打包成jar-->
<target name="jar" >
<jar destfile="${build.dist.dir}/${project.name}-model-${version}.jar">
<fileset dir="${build.classes.dir}/com/model" includes="**/*.*"></fileset>
</jar>
<jar destfile="${build.dist.dir}/${project.name}-dao-${version}.jar">
<fileset dir="${build.classes.dir}/com/dao" includes="**/*.*"></fileset>
</jar>
<jar destfile="${build.dist.dir}/${project.name}-service-${version}.jar">
<fileset dir="${build.classes.dir}/com/service" includes="**/*.*"></fileset>
</jar>
</target>
</project>
测试项目源码:
http://download.csdn.net/detail/wobendiankun/7415063