build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="webService2" default="" basedir=".">
	<property environment="env" />
	<!-- 链接库目录 -->
	<property name="lib.dir" value="${basedir}/lib" />
	<!-- 工程目录 -->
	<property name="src" value="${basedir}/src" />
	<property name="test" value="${basedir}/test" />
	<!-- 工程编译目录 -->
	<property name="build.dir" value="${basedir}/build" />
	<property name="wsdl.dir" value="${basedir}/resources/META-INF" />
	<property name="wsdl.uri" value="${wsdl.dir}/BankWebService.wsdl" />
	<property name="service.target" value="build/service" />
	<property name="client.target" value="build/client" />
	<!-- Tomcat目录 -->
	<property name="tomcat.dir" value="/opt/javaworkspace/tomcat" />
	<property name="tomcatlib.dir" value="${tomcat.dir}/lib" />
	<!-- 测试编译目录 -->
	<property name="test.dir" value="${build.dir}/test" />
	<property name="report.dir" value="${build.dir}/Report" />
	<property name="junitreport.dir" value="${report.dir}/JunitReport" />
	<property name="finalreport.dir" value="${report.dir}/FinalReport" />
	<!-- 数据库 -->
	<property file="${basedir}/config/datasource.properties" />
	<property name="changelog" value="${basedir}/database/database.changelog.xml" />

	<!-- ant-contrib.jar -->
	<taskdef resource="net/sf/antcontrib/antlib.xml">
		<classpath>
			<pathelement location="${tomcatlib.dir}/ant-contrib-0.6.jar" />
		</classpath>
	</taskdef>

	<!-- 链接库(jar;class文件) -->
	<path id="base.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${tomcatlib.dir}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<!-- 清理 -->
	<target name="clean">
		<delete dir="${build.dir}" />
		<delete dir="${basedir}/../tomcat/webapps/axis2/WEB-INF/classes/com" />
		<delete file="${basedir}/../tomcat/webapps/axis2/WEB-INF/services/BankWebService.aar" />
		<delete file="${basedir}/lib/userservice.jar" />
		<delete file="${basedir}/resources/META-INF/UserServiceImpl.wsdl" />
		<delete file="${basedir}/../tomcat/webapps/axis2/WEB-INF/lib/BankWebService.jar" />
	</target>

	<!-- 创建目录,拷贝文件 -->
	<target name="init" depends="clean">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${report.dir}" />
		<mkdir dir="${test.dir}" />
		<mkdir dir="${report.dir}" />
		<mkdir dir="${junitreport.dir}" />
		<mkdir dir="${finalreport.dir}" />
		<mkdir dir="${service.target}/classes" />
		<copy todir="${service.target}/classes">
			<fileset dir="${basedir}/config">
				<include name="*.xml" />
				<include name="*.properties" />
			</fileset>
		</copy>
		<copy todir="${test.dir}">
			<fileset dir="${basedir}/config">
				<include name="*.xml" />
				<include name="*.properties" />
			</fileset>
		</copy>
	</target>

	<!-- 编译service -->
	<target name="compile.service" depends="init">
		<javac debug="on" fork="true" destdir="${service.target}/classes" srcdir="${basedir}/src" classpathref="base.classpath">
		</javac>
		<copy todir="${service.target}/classes/com/goodhope/pojo">
			<fileset dir="${basedir}/src/com/goodhope/pojo">
				<include name="*.xml" />
			</fileset>
		</copy>
	</target>

	<!-- 编译test -->
	<target name="compile.test" depends="compile.service">
		<javac srcdir="${test}" destdir="${test.dir}">
			<classpath path="${service.target}/classes" />
			<classpath refid="base.classpath" />
		</javac>
	</target>

	<!-- 单元测试-生成单元测试报告 -->
	<target name="junit.test" depends="compile.test">
		<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
			<classpath>
				<pathelement location="${test.dir}" />
				<pathelement location="${service.target}/classes" />
				<path refid="base.classpath" />
			</classpath>
			<formatter type="xml" />
			<batchtest todir="${junitreport.dir}" fork="true">
				<fileset dir="${test.dir}">
					<include name="**/*Test.*" />
				</fileset>
			</batchtest>
		</junit>
		<junitreport todir="${junitreport.dir}">
			<fileset dir="${junitreport.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${junitreport.dir}" />
		</junitreport>
		<fail if="tests.failed">
		</fail>
	</target>

	<!-- 将Service打成 jar包 -->
	<target name="tojar" depends="junit.test">
		<jar destfile="${service.target}/BankWebService.jar">
			<fileset dir="${service.target}/classes">
				<exclude name="*.xml" />
				<exclude name="*.properties" />
			</fileset>
		</jar>
	</target>

	<!-- 拷贝jar文件 -->
	<target name="copy.jar.file" depends="tojar">
		<copy file="${service.target}/BankWebService.jar" toDir="${basedir}/lib" overwrite="false" />
		<copy file="${service.target}/BankWebService.jar" toDir="${basedir}/../tomcat/webapps/axis2/WEB-INF/lib" overwrite="true" />
		<copy todir="${basedir}/../tomcat/webapps/axis2/WEB-INF/classes" overwrite="true">
			<fileset dir="${basedir}/config">
				<include name="*.xml" />
				<include name="*.properties" />
			</fileset>
		</copy>
		<copy todir="${basedir}/../tomcat/webapps/axis2/WEB-INF/lib" overwrite="false">
			<fileset dir="${basedir}/lib">
				<include name="*.jar" />
			</fileset>
		</copy>
	</target>

	<!-- 生成wsdl -->
	<target name="generate.wsdl" depends="copy.jar.file">
		<taskdef name="java2wsdl" classname="org.apache.ws.java2wsdl.Java2WSDLTask" classpathref="base.classpath" />
		<java2wsdl className="com.goodhope.webservice.BankWebService" outputLocation="${wsdl.dir}" targetNamespace="http://goodhope.com/" schemaTargetNamespace="http://goodhope.com/xsd" locationuri="http://localhost:8099/axis2/services/BankWebService">
			<classpath>
				<pathelement path="${base.classpath}" />
				<pathelement location="${service.target}/classes" />
			</classpath>
		</java2wsdl>
	</target>

	<!--生成 arr -->
	<target name="generate.arr" depends="generate.wsdl">
		<copy toDir="${service.target}/classes" failonerror="false">
			<fileset dir="${basedir}/resources">
				<include name="**/*.xml" />
			</fileset>
		</copy>
		<jar destfile="${service.target}/BankWebService.aar">
			<fileset dir="${service.target}/classes" />
		</jar>
	</target>

	<!-- 根据 wsdl生成 服务端代码 -->
	<target name="generate.service" depends="generate.arr">
		<java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="base.classpath">
			<arg line="-uri ${wsdl.uri}" />
			<arg line="-s" />
			<arg line="-ss" />
			<arg line="-sd" />
			<arg line="-ssi" />
			<arg line="-ap" />
			<arg line="-ns2p http://goodhope.com/xsd=com.goodhope.webservice.xsd" />
			<arg line="-l java" />
			<arg line="-p com.goodhope.webservice" />
			<arg line="-d adb" />
			<arg line="-o ${build.dir}/service" />
		</java>
		<copy file="${basedir}/src/com/goodhope/webservice/BankWebService.java" toDir="${build.dir}/service/src/com/goodhope/webservice" overwrite="yes" />
		<copy file="${basedir}/resources/META-INF/services.xml" toDir="${build.dir}/service/resources/" overwrite="yes">
		</copy>
		<ant dir="${build.dir}/service">
			<property name="axis2.home" value="${basedir}" />
		</ant>
		<copy file="${service.target}/build/lib/BankWebService.aar" toDir="${basedir}/../repository/services" overwrite="yes" />
		<copy file="${service.target}/build/lib/BankWebService.aar" toDir="${basedir}/../tomcat/webapps/axis2/WEB-INF/services" overwrite="yes" />
	</target>

	<!-- 根据 wsdl生成 客户端代码 -->
	<target name="generate.client" depends="generate.arr">
		<delete dir="${client.target}" />
		<mkdir dir="${client.target}" />
		<java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="base.classpath">
			<arg line="-uri ${wsdl.uri}" />
			<arg line="-s" />
			<arg line="-ns2p http://goodhope.com/xsd=com.goodhope.webservice.xsd" />
			<arg line="-l java" />
			<arg line="-p com.goodhope.webservice" />
			<arg line="-d adb" />
			<arg line="-o ${build.dir}/client" />
		</java>
		<ant dir="${build.dir}/client">
			<property name="axis2.home" value="${basedir}" />
		</ant>
		<copy file="${service.target}/BankWebService.aar" toDir="${basedir}/../tomcat/webapps/axis2/WEB-INF/services" overwrite="yes" />
	</target>

	<!-- all -->
	<target name="all" depends="tomcat.stop,init,generate.client" />

	<!-- 启动 Tomcat -->
	<target name="tomcat.start">
		<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.start" inheritall="false" />
	</target>

	<!-- 停止 Tomcat -->
	<target name="tomcat.stop">
		<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.stop" inheritall="false" />
	</target>

	<!-- 刷数据库 -->
	<target name="update-database">
		<taskdef name="updateDatabase" classname="liquibase.ant.DatabaseUpdateTask" classpathref="base.classpath" />
		<updateDatabase changeLogFile="${changelog}" driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/bank" username="goodhope" password="goodhope" dropFirst="true" classpathref="base.classpath" />
	</target>

	<!-- 配置文件 -->
	<taskdef resource="migratortasks.properties" classpathref="base.classpath" />

	<!-- 指定执行文件位置 -->
	<macrodef name="excuteScript">
		<attribute name="dir" />
		<attribute name="dropFirst" default="false" />
		<sequential>
			<migrateDatabase defaultschemaname="public" changeLogFile="@{dir}/script_suite.xml" driver="${dirver}" url="${url}" username="${username}" password="${password}" dropFirst="@{dropFirst}" classpathref="base.classpath" />
		</sequential>
	</macrodef>

	<!-- 删除数据库表,并重新建立数据表 -->
	<target name="fresh.bank.data" description="drop all table and rebuild schema">
		<excuteScript dir="${basedir}/database/script/before" dropFirst="true" />
		<excuteScript dir="${basedir}/database/script/create_table" />
		<excuteScript dir="${basedir}/database/data" />
	</target>
</project>

你可能感兴趣的:(tomcat,xml,ant,单元测试)