技术开发 频道

webservice学习笔记——提高篇



六、一个通用的ant脚本
1、脚本内容
  上面的buildtest.xml脚本虽然对原始的build.xml做了精简,但是还不够通用,
比如服务器的类名和方法名等等都写死了,显然还不适合我们的需要,于是我又写了一个相对通用的脚本
mybuild.xml,内容如下

<?xml version="1.0" encoding="UTF-8"?> <project name="hello-jaxws" default="all" basedir="."> <!-- javaee安装目录的设置,代替了原来脚本中的导入语句 --> <property name="javaee.home" value = "d:/Sun/SDK"/> <property name="javaee.domaindir" value="${javaee.home}/domains/domain1"/> <property name="javaee.server.name" value="localhost"/> <property name="javaee.server.port" value="8080"/> <!-- 设置发布目录和类的输出目录 --> <property name="autodeploydir" value="${javaee.domaindir}/autodeploy"/> <property name="classesdir" value="./build"/> <!-- 设置java类库路径 --> <path id="classpath"> <pathelement location="${javaee.home}/lib/j2ee.jar"/> <pathelement location="${classesdir}"/> </path> <!-- 提供服务的类名 --> <property name="serviceclass" value="Hello" /> <property name="serverclasspath" value="endpoint" /> <!-- 服务器所在的package名称 也就是Hello.java文件中第一句 package endpoint;语句中的endpoint --> <!-- 调用webservice的测试类名--> <property name = "clientclass" value="Client"/> <property name = "clientclasspath" value="client"/> <!-- 测试类所在的package名称, 也就是Client.java文件中第一句 package client;语句中的client; --> <!-- 生成的wsdl文件的uri的格式,注意value的值分成两行写时有问题, 这里为了阅读方便而分行,拷贝时必须放到一行 我还没找到ant脚本里的续行符是什么:-( --> <property name="wsdluri" value="http://${javaee.server.name}:${javaee.server.port} /${serviceclass}/${serviceclass}Service?WSDL" /> <target name="all" depends="run-client"> <!-- antcall target="restore"/ --> </target> <target name="run-client" depends="compile-client,run-client-windows" /> <!-- 运行测试类,为项目的最后一个步骤--> <target name="run-client-windows"> <exec executable="${javaee.home}/bin/appclient.bat" dir="${classesdir}"> <arg value="${clientclasspath}.${clientclass}" /> </exec> </target> <!-- 编译测试webservice的客户类,为项目的第三个步骤,本步骤的输出文件为 ${classesdir}/client/Client.class --> <target name="compile-client" depends="get-artifacts"> <javac srcdir="./src/${clientclasspath}" destdir="${classesdir}"> <classpath refid="classpath" /> </javac> </target> <target name="get-artifacts" depends="compile-deploy- ②asadmin deploy --user admin --password laomailaomai --host localhost --port 4848 --contextroot hello --upload=true --target server service,get-artifacts-windows"/> <!-- 生成客户端的stub类 --> <target name="get-artifacts-windows"> <exec executable="${javaee.home}/bin/wsimport.bat"> <arg line="-keep -d ${classesdir} ${wsdluri}" /> </exec> </target> <!-- 本步骤的目的是编译服务器类,并自动产生wsdl文件--> <target name="compile-deploy-service"> <echo message="${javaee.home}" /> <javac srcdir="./src" includes="${serverclasspath}/**" destdir="${autodeploydir}" classpath="${javaee.home}/lib/j2ee.jar" /> <waitfor maxwait="100" maxwaitunit="second"> <or> <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployed"/> <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployFailed"/> </or> </waitfor> <condition property="deploy_succeeded"> <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployed"/> </condition> <condition property="deploy_failed"> <available file="${autodeploydir}/${serverclasspath}/${serviceclass}.class_deployFailed"/> </condition> </target> <!-- 以下的任务用于清理和卸载--> <target name="clean"> <delete dir="${autodeploydir}/${serverclasspath}"/> <delete dir="${classesdir}/${serverclasspath}"/> <delete dir="${classesdir}/${clientclasspath}"/> </target> </project>


2、脚本的使用方式
以后再写自己service时,只要按实际情况改变以下4条语句中的value值就可以了
<property name="serviceclass" value="Hello" /> 
<property name="serverclasspath" value="endpoint" />
<property name = "clientclass" value="Client"/>
<property name = "clientclasspath" value="client"/> 

0
相关文章