首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

webservice学习札记 提高篇

2012-12-23 
webservice学习笔记 提高篇精简后的ant脚本1、卸载webservice 执行原build.xml里的clean和 undeploy任务,把

webservice学习笔记 提高篇
精简后的ant脚本
1、卸载webservice
执行原build.xml里的clean和 undeploy任务,把安装好的webservice删除掉,具体办法为:
(1)在myeclipse里打开build.xml文件
(2)在build.xml文件里单击右键菜单中的""run as"->"2 ant build..."",
(3)在弹出的对话框中只选择clean和undelpoy任务。 然后单击"run"按钮。
此时再访问http://localhost:8080/Hello/HelloService?WSDL,出现http 404错误,说明卸载成功
2、简化后的脚本内容,在hello-jaxws目录下新建一个buildtest.xml文件,内容为



3、运行结果
在myeclipse里执行这个刚才编写的buildtest.xml脚本,console里的输出为
Buildfile: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\buildtest.xml
compile-deploy-service:
    [mkdir] Created dir: D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
     [echo] d:/Sun/SDK
    [javac] Compiling 1 source file to D:\Sun\SDK\domains\domain1\autodeploy
get-artifacts-windows:
get-artifacts:
compile-client:
    [javac] Compiling 1 source file to D:\Sun\SDK\samples\javaee5\webservices\hello-jaxws\build
run-client-windows:
     [exec] Hello result = Hello Administrator!
run-client:
all:
BUILD SUCCESSFUL
Total time: 50 seconds

也执行成功


一个通用的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"/>

热点排行