初步使用ant的简单配置

在Eclipse下运行Ant的配置

  1. 属性值的配置文件build.properties(也可以把属性直接写在build.xml文件中)
    libjar = StudentManager/WEB-INF/lib/ 
  2. build.xml配置文件
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE project>
    <project name="DynamicWebProject" default="junit-and-report">
    
    	<property environment="env" />
    	<property file="build.properties" description="包含外部的属性文件" />
    	<property name="disk.path" value="${user.home}" />
    	<property name="project.dir" value="${basedir}" description="指定当前的本目录" />
    	<property name="src.dir" value="${project.dir}/src" />
    	<property name="classes.dir" value="${project.dir}/build/classes/" />
    	<property name="jar.dir" value="${project.dir}/${libjar}" />
    	<property name="website.dir" value="${project.dir}/StudentManager/" />
    	<property name="doc.dir" value="${project.dir}/javadoc">
    	</property>
    	<property name="report.dir" value="${project.dir}/report">
    	</property>
    
    
    	<path id="web-class-path" description="工程字节码(类)路径">
    		<pathelement location="${classes.dir}" />
    		<fileset dir="${jar.dir}">
    			<include name="*.jar" />
    		</fileset>
    	</path>
    
    	<path id="web-code-path" description="工程源代码路径">
    		<pathelement location="${src.dir}" />
    	</path>
    
    	<!-- 这个用法:相当于定义一个函数,可供其他的target直接调用 -->
    	<macrodef name="run-main-mathoud" description="定义一个运行main方法的'函数'">
    		<attribute name="classname" />
    		<attribute name="args" />
    		<sequential>
    			<echo message="运行类:@{classname}里的main方法">
    			</echo>
    			<java classname="@{classname}" fork="true" description="注意:java命令运行的是class字节码,而不是java源代码!必须先编译源代码成字节码方可运行java命令">
    				<classpath refid="web-class-path">
    				</classpath>
    				<jvmarg value="-Xmx1024m" />
    				<jvmarg value="-Xms512m" />
    				<arg value="@{args}" />
    			</java>
    			<echo message="完成运行类:@{classname}里的main方法">
    			</echo>
    		</sequential>
    	</macrodef>
    
    	<target name="test-run">
    		<echo message="运行自定义的ant函数">
    		</echo>
    		<run-main-mathoud classname="com.dchaoxiong.studentmanage.test.anttest.TestAnt" />
    		<echo message="运行完成">
    		</echo>
    	</target>
    
    
    
    	<target name="buildwar" depends="copyfiles" description="把网站打包成WAR包">
    		<echo message="创建网站的WAR包">
    		</echo>
    		<war destfile="${website.dir}/website.war" webxml="${website.dir}/WEB-INF/web.xml">
    			<fileset dir="${website.dir}">
    				<exclude name="WEB-INF/web.xml" />
    			</fileset>
    		</war>
    		<echo message="打包完成">
    		</echo>
    	</target>
    
    	<target name="java-to-class" description="把java源代码编译成class字节码">
    		<mkdir dir="${classes.dir}" />
    		<echo message="开始编译java源代码成字节码">
    		</echo>
    		<javac srcdir="${src.dir}" destdir="${classes.dir}" encoding="utf-8">
    			<classpath refid="web-class-path">
    			</classpath>
    		</javac>
    		<echo message="编译完成">
    		</echo>
    	</target>
    
    	<target name="copyfiles" depends="java-to-class">
    		<echo message="拷贝文件到相应的目录">
    		</echo>
    		<copydir dest="${classes.dir}" src="${src.dir}">
    		</copydir>
    		<copydir dest="${website.dir}/WEB-INF/classes/" src="${classes.dir}">
    		</copydir>
    		<echo message="拷贝完成">
    		</echo>
    	</target>
    
    
    	<target name="create-javadoc">
    		<echo message="通过java源代码生成javadoc文档">
    		</echo>
    		<delete dir="${doc.dir}" />
    		<mkdir dir="${doc.dir}" />
    
    		<javadoc destdir="${doc.dir}" author="true" version="true" use="true" additionalparam="-encoding UTF-8 -charset UTF-8" nodeprecated="true" nodeprecatedlist="true" noindex="true" nonavbar="true" notree="true" access="public" source="1.5" windowtitle="学生管理系统javadoc文档" linksource="true">
    
    			<packageset dir="${src.dir}" defaultexcludes="yes">
    				<include name="**/**" />
    			</packageset>
    			<classpath refid="web-class-path" />
    		</javadoc>
    		<echo message="文档已经生成">
    		</echo>
    	</target>
    
    
    
    	<target name="junit-and-report">
    		<echo message="Junit和Ant结合运行工程里所有测试的程序">
    		</echo>
    
    		<delete dir="${report.dir}">
    		</delete>
    		<mkdir dir="${report.dir}" />
    
    		<junit printsummary="on" fork="true" showoutput="true">
    			<sysproperty key="ant.home" value="${ant.home}" />
    			<classpath refid="web-class-path">
    			</classpath>
    			<formatter type="xml" />
    			<batchtest todir="${report.dir}">
    				<fileset dir="${classes.dir}">
    					<include name="**/*Test.*" />
    				</fileset>
    			</batchtest>
    		</junit>
    
    		<junitreport todir="${report.dir}">
    			<fileset dir="${report.dir}">
    				<include name="TEST-*.xml" />
    			</fileset>
    			<report format="frames" todir="${report.dir}" />
    		</junitreport>
    
    		<echo message="Junit测试完成">
    		</echo>
    
    	</target>
    
    
    	<target name="deploy-to-localhost">
    		<delete file="${env.TOMCAT_HOME}/webapps/com.hotye.website.war" />
    		<delete dir="${env.TOMCAT_HOME}/webapps/com.hotye.website" />
    		<delete dir="${env.TOMCAT_HOME}/work/Catalina/localhost/com.hotye.website" />
    		<copy todir="${env.TOMCAT_HOME}/webapps/">
    			<fileset file="build/com.hotye.website.war" />
    		</copy>
    	</target>
    
    	<target name="deploy-to-131.1.2.43">
    		<echo message="把相应文件拷贝到远程的例子">
    		</echo>
    		<delete file="\\131.1.2.43\server\com.hotye.website.war" />
    		<copy todir="\\131.1.2.43\server\com.hotye.website.war" file="\\131.1.2.43\webapps\com.hotye.website.war" />
    		<delete file="\\131.1.2.43\webapps\com.hotye.website.war" />
    		<copy todir="\\131.1.2.43\webapps">
    			<fileset dir="build/" includes="com.hotye.website.war" />
    		</copy>
    	</target>
    
    
    </project>
     

你可能感兴趣的:(tomcat,Web,ant,配置管理,JUnit)