ANT通用模板

“遍身罗绮者,不是养蚕人。程序员为别人写程序,却鲜有时间让自己的工序自动化,这不是件很滑稽的事情吗?”

记得一句话,这个世界是以懒惰为驱动力的。人类发明各项工具,都是为了让人们生活更加舒适、轻松。(当然,更恰当的说法是一小撮人为了让大部分人的生活更加EASY而去努力创新,然后大部分人为其买单。)重复性的操作有时的确很烦人,如果使用工具能将人从繁琐的工作中解脱出来,节省了时间而提高了效率,何乐而不为呢?

挺久前就想总结一下自己这阵子使用ANT的经历,这两天重温了一下相关的书籍,就在这留些记录吧。

什么是ANT?Ant是一种基于Java的build工具。其本身是一个流程脚本引擎,通编写相应的配置文件(通常为build.xml),可以实现相关项目的编译、打包及测试待操作的自动化,从而大大提高了开发过程的效率,也使得项目文件的平台信赖性降低。

只要在配置文件中定义好相关的target标签,到时运行相应的target名就可以完成该任务,而对于一般的项目而言,大部分的任务都是相同的。根据所看到的资料,整理出来一份比较合适我现在用的模板。

<?xml version="1.0" encoding="UTF-8"?>



<!--

This file defines the basic targets of a ant build.xml file.

Copy it to the project folder and change project name and product name first.

-->

<project name="ProjectName" default="compile" basedir=".">

	<property name="jarfile.name" value="jarfile" />



	<property name="build.dir" location="build" />

	<property name="build.product.dir" location="${build.dir}/product" />

	<property name="build.classes.dir" location="${build.dir}/classes" />

	<property name="build.test.dir" location="${build.dir}/test" />

	<property name="build.doc.dir" location="${build.dir}/doc" />

	<property name="build.lib.dir" location="${build.dir}/lib" />



	<property name="doc.dir" location="doc" />

	<property name="index.dir" location="index" />

	<property name="src.dir" location="src" />

	<property name="test.dir" location="${src.dir}/test" />

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



	<path id="build.classpath">

		<pathelement location="${build.classes.dir}" />

		<fileset dir="${build.lib.dir}">

			<include name="*.jar" />

		</fileset>

	</path>



	<path id="build.test.classpath">

		<pathelement location="${build.test.dir}" />

		<pathelement location="${build.classes.dir}" />

		<fileset dir="${build.lib.dir}">

			<include name="*.jar" />

		</fileset>

	</path>



	<path id="project.classpath">

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

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

			<include name="*.jar" />

		</fileset>

	</path>

	<!-- ================================= 

          target: prepare              

         ================================= -->

	<target name="prepare" description="Prepare project for building">

		<mkdir dir="${build.product.dir}" />

		<mkdir dir="${build.classes.dir}" />

		<mkdir dir="${build.test.dir}" />

	</target>



	<!-- ================================= 

          target: compile              

         ================================= -->

	<target name="compile" depends="prepare" description="Compile source code">

		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on">

			<exclude name="**/*Test.java" />

			<exclude name="**/Test*.java" />

		</javac>

	</target>



	<target name="compile-tests" depends="prepare" description="Compile Test source code">

		<javac srcdir="${src.dir}" destdir="${build.test.dir}" debug="on">

			<include name="**/*Test.java" />

			<include name="**/Test*.java" />

		</javac>

	</target>



	<!-- - - - - - - - - - - - - - - - - - 

          target: copy-lib   copy-libs to build.lib.dir                   

         - - - - - - - - - - - - - - - - - -->

	<target name="copy-lib">

		<mkdir dir="${build.lib.dir}" />

		<mkdir dir="${lib.dir}" />

		<copy todir="${build.lib.dir}">

			<fileset dir="${lib.dir}">

				<include name="**/*.jar" />

			</fileset>

		</copy>

	</target>



	<!-- ================================= 

          target: test              

         ================================= -->

	<target name="test" depends="compile-tests,copy-lib" description="Test source code">

		<junit printsummary="on" haltonfailure="true" failureproperty="tests.failed" showoutput="true">

			<classpath refid="build.test.classpath" />

			<formatter type="brief" usefile="false" />

			<batchtest>

				<fileset dir="${build.test.dir}">

					<include name="**/*Test.class" />

				</fileset>

			</batchtest>

			<sysproperty key="doc.dir" value="${doc.dir}" />

			<sysproperty key="index.dir" value="${index.dir}" />

		</junit>

	</target>



	<!-- ================================= 

          target: clean              

         ================================= -->

	<target name="clean" description="Clean the build directory">

		<delete dir="${build.dir}" />

	</target>



	<!-- ================================= 
          target: jar              
         ================================= -->
	<target name="jar" depends="compile,copy-lib" description="Pack compiled class files into a jar">
		<jar destfile="${build.product.dir}/${jarfile.name}.jar">

			<fileset dir="${build.classes.dir}">
				<include name="**/*.class" />
				<exclude name="**/*Test.class" />

				<exclude name="**/Test*.class" />
			</fileset>

			<!--	<fileset dir="${build.lib.dir}">

				<include name="**/*.jar" />

			</fileset>

			-->
		</jar>
	</target>


	<!-- ================================= 
          target: javadoc              
         ================================= -->
	<target name="javadoc" description="Generates java docs">

		<mkdir dir="${build.doc.dir}"/>

		<javadoc sourcepath="${src.dir}" destdir="${build.doc.dir}" Encoding="utf8">

			

		</javadoc>

	</target>
</project>

平时一般的小项目用这个就差不多了。一会有空再增加一个javadoc功能来实现指定locale生成doc的任务。

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: