Ant脚本文件build.xml模版

本文展示了Ant构建工具的一个典型build.xml文件模版,涵盖了初始化、编译源码、打包成jar、运行JUnit测试、生成报告、构建Web应用、打包成WAR文件以及发布到Tomcat等步骤。通过这个模版,可以了解Ant如何管理和执行Java项目的构建流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<project name="myproject" basedir="." default="junit">

	<property name="src.dir" value="src" />
	<property name="classes.dir" value="bin" />
	<property name="jar.dir" value="jar" />
	<property name="srctest.dir" value="srctest" />
	<property name="classestest.dir" value="bintest" />
	<property name="lib.dir" value="lib" />
	<property name="report.dir" value="report" />
	
	<property name="war.dir" value="war" />
	<property name="classesweb.dir" value="WebContent/WEB-INF/classes" />
	<property name="tomcat.dir" value="/home/programmer/tomcat" />
	

	<path id="application-lib">
		<fileset dir="${lib.dir}" includes="**/*.jar" />
	</path>
	
	<path id="tomcat-lib">
		<fileset dir="${tomcat.dir}/lib" includes="**/*.jar" />
	</path>

	<target name="init">
		<echo>init...</echo>
		<delete dir="${classes.dir}" />
		<delete dir="${jar.dir}" />
		<delete dir="${classestest.dir}" />
		<delete dir="${report.dir}" />
		<delete dir="${war.dir}" />
		<delete dir="${classesweb.dir}" />
	</target>

	<target name="compile" depends="init">
		<echo>compile source code</echo>
		<mkdir dir="${classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="on" encoding="utf-8">
			<classpath>
				<path refid="application-lib" />
			</classpath>
		</javac>
		<copy todir="${classes.dir}">
			<fileset dir="${src.dir}" includes="**/log4j.properties" />
		</copy>
	</target>
	
	<target name="makejar" depends="compile">
		<echo>make jar file</echo>
		<mkdir dir="${jar.dir}" />
		<jar destfile="${jar.dir}/myproject.jar">
			<fileset dir="${classes.dir}">
			</fileset>
		</jar>
    </target>

	<target name="compiletest" depends="compile">
		<echo>compile test source code</echo>
		<mkdir dir="${classestest.dir}" />
		<javac srcdir="${srctest.dir}" destdir="${classestest.dir}" includeantruntime="on" encoding="utf-8">
			<classpath>
				<pathelement location="${classes.dir}" />
				<path refid="application-lib" />
			</classpath>
		</javac>
		<copy todir="${classestest.dir}">
			<fileset dir="${srctest.dir}" includes="**/log4j.properties" />
		</copy>
	</target>

	<target name="junit" depends="compiletest">
		<echo>run junit code</echo>
		<mkdir dir="${report.dir}" />
		<junit printsummary="yes" failureproperty="junit.tests.failure" showoutput="on">
			<classpath>
				<path location="${classes.dir}" />
				<path location="${classestest.dir}" />
				<path refid="application-lib" />
			</classpath>
			<formatter type="xml" />
			<jvmarg value="-Djava.library.path=." />
			<batchtest fork="yes" todir="${report.dir}">
				<fileset dir="${classestest.dir}" includes="**/test/*Test.class" />
			</batchtest>
		</junit>
		<junitreport todir="${report.dir}">
			<fileset dir="${report.dir}">
				<include name="**/TEST-*.xml" />
			</fileset>
			<report todir="${report.dir}" />
		</junitreport>
	</target>
	
	<target name="compileweb" depends="init">
		<echo>compile web source code</echo>
		<mkdir dir="${classesweb.dir}" />
		<javac srcdir="${src.dir}" destdir="${classesweb.dir}" includeantruntime="on" encoding="utf-8">
			<classpath>
				<path refid="tomcat-lib" />
				<path refid="application-lib" />
			</classpath>
		</javac>
		<copy todir="${classesweb.dir}">
			<fileset dir="${src.dir}" includes="**/log4j.properties" />
		</copy>
	</target>
	
	<target name="war" depends="compileweb">  
        <echo>make war file</echo>  
        <mkdir dir="${war.dir}" />  
        <war warfile="${war.dir}/myproject.war" webxml="WebContent/WEB-INF/web.xml">  
            <lib dir="${lib.dir}" />  
            <classes dir="${classesweb.dir}" />  
            <fileset dir="WebContent" />  
        </war>  
    </target>  
	
	<target name="publish" depends="war">  
        <echo>publish war file to tomcat</echo>  
        <copy todir="${tomcat.dir}/webapps">  
            <fileset dir="${war.dir}" includes="myproject.war" />  
        </copy>  
    </target>  

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值