We shall build the web service using an Apache Ant build.xml
file. To learn more about Apache Ant and creating build files refer to the URL http://ant.apache.org/. Create a build.xml
file in the project root directory. Select File | New File. In New File select Categories as Other and File Types as Ant Build Script, and click on Next:
In the Name and Location window, click on the Browse button for Folder. In the Browse Folders window, select the project root folder HelloWS, and click on Select Folder:
In Name and Location, specify the File Name as build, Project as HelloWS, and click on Finish:
A build.xml file gets added to the HelloWS folder:
The build.xml
file is used to compile, package, and deploy the web service to the GlassFish Server, use the -clientjar
option to generate a JAR file for the web service portable artifacts and WSDLs, compile the client class, and run the client. In the build.xml
file, add property elements for the properties listed in the following table, which also includes properties from the deploy-targets.xml
build fragment:
Property |
Value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Create a path
element to specify the classpath, which includes the tools.jar
file from Java 7 and the JAR files from the GlassFish modules
directory:
<path id="classpath"> <pathelement location="${java.home}/lib/tools.jar"/> <fileset dir="${modules.home}"> <include name="*.jar"/> </fileset> </path>
In build.xml
, we shall specify targets for the following:
wsimport
task. -clientjar
option of wsimport
. HelloWSClient.java
.Specify the targets in build.xml
listed in the following table, which also lists the targets from the deploy-targets.xml
file:
Target |
Description |
---|---|
setup |
Build the required directories. |
build-server-wsdl |
Compiles the web service classes in the |
create-war |
Creates a WAR file from the compiled classes and includes the |
generate-client |
Generates the client JAR using the wsimport's |
client |
Compiles the client class. |
run |
Runs the client class with the client JAR generated using the |
deploy |
Copies the web service WAR file to the GlassFish |
server |
Invokes the |
clean |
Deletes the |
The build.xml
file is used to perform the following tasks:
Add a task definition for the com.sun.tools.ws.ant.WsImport
class, which defines the wsimport
task:
<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport"> <classpath refid="classpath"/> </taskdef>
The new -clientjar
option is not needed for the web service to get deployed on the GlassFish Server. In the build-server-wsdl
target, run the wsimport
task to generate the web service artifacts from the web service WSDL. Also, compile the web service implementation class:
<target name="build-server-wsdl" depends="setup"> <wsimport debug="true" verbose="${verbose}" keep="false" destdir="${build.classes.home}" package="hellows" wsdl="${basedir}/config/HelloWSService.wsdl"> </wsimport> <javac fork="true" srcdir="${basedir}/src" destdir="${build.classes.home}" includes="**/hellows/**"> <classpath refid="classpath"/> </javac> </target>
In the create-war
target, package the web service classes including the deployment descriptors into a WAR file:
<target name="create-war"> <war warfile="${build.war.home}/${ant.project.name}.war" webxml="config/web.xml"> <webinf dir="${basedir}/config" includes="sun-jaxws.xml"/> <zipfileset dir="${basedir}/config" includes="*.wsdl, *.xsd" prefix="WEB-INF/wsdl"/> <classes dir="${build.classes.home}" includes="**/*.class"/> </war> </target>
In the server
target invoke clean, build-server-wsdl, create-war
, and the deploy
target from the deploy-targets.xml
file to compile, package, and deploy the web service:
<target name="server" depends="setup"> <antcall target="clean"/> <antcall target="build-server-wsdl"/> <antcall target="create-war"/> <antcall target="deploy"/> </target>
Next, we demonstrate the new -clientjar
option in wsimport
to generate a JAR file for the web service portable artifacts and WSDLs to be made available to the JAX-WS runtime when the web service is invoked from a client. In the generate-client
target, run the wsimport
task and specify the JAR file to package the generated artifacts and WSDLs using the clientjar
attribute. Specify the WSDL URL as http://localhost:8080/clientjar/hellows?wsdl
with the wsdl
attribute. In the wsdl
URL, clientjar
is the WAR file name for the deployed web service and hellows
is the servlet pattern to invoke the web service servlet as specified in web.xml
. As the JAR file is to be made available to the client at runtime, specify the package name the same as the package for the client class:
<target name="generate-client"> <wsimport debug="true" verbose="${verbose}" destdir="${build.classes.home}" package="hellowsclient" clientjar="HelloWSServiceClient.jar" wsdl="http://localhost:8080/clientjar/hellows?wsdl"> </wsimport> </target>
Next, compile the web service client class hellowsclient.HelloWSClient.java
. In the client
target compile the client class using the javac
task with the JAR file generated using the -clientjar
option in the classpath:
<target name="client" depends="generate-client"> <javac fork="true" srcdir="${basedir}/src" destdir="${build.classes.home}" includes="/hellowsclient/**"> <classpath> <path refid="classpath"/> <pathelement location="${build.classes.home} /HelloWSServiceClient.jar"/> </classpath> </javac> </target>
To learn more about the Wsimport Ant
task in Java 7 refer to the URL http://jax-ws.java.net/nonav/2.2.5/docs/wsimportant.html.
Next, we run the client class in the run
target using the java
task with the JAR file generated using the -clientjar
option in the classpath:
<target name="run"> <java fork="true" classname="hellowsclient.HelloWSClient"> <classpath> <path refid="classpath"/> <pathelement location="${build.classes.home}"/> <pathelement location="${build.classes.home} /HelloWSServiceClient.jar"/> <pathelement location="${basedir}/config"/> </classpath> <jvmarg value="-Dcom.sun.xml.ws.transport. http.client.HttpTransportPipe.dump=${log}"/> </java> </target>
The build.xml
is listed as follows:
<?xml version="1.0" encoding="UTF-8"?> <project basedir="." default="help" name="clientjar"> <property name="java.home" value="C:/Program Files/Java/jdk1.7.0" /> <property name="modules.home" value="C:/glassfish3/glassfish/modules" /> <import file="config/deploy-targets.xml"/> <path id="classpath"> <pathelement location="${java.home}/lib/tools.jar"/> <fileset dir="${modules.home}"> <include name="*.jar"/> </fileset> </path> <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport"> <classpath refid="classpath"/> Apache Ant build file, creatingclient, running</taskdef> <target name="setup"> <mkdir dir="${build.home}"/> <mkdir dir="${build.classes.home}"/> <mkdir dir="${build.war.home}"/> </target> <target name="clean"> <delete dir="${build.home}" includeEmptyDirs="true"/> </target> <target name="build-server-wsdl" depends="setup"> <wsimport debug="true" verbose="${verbose}" keep="false" destdir="${build.classes.home}" package="hellows" wsdl="${basedir}/config/HelloWSService.wsdl"> </wsimport> <javac fork="true" srcdir="${basedir}/src" destdir="${build.classes.home}" includes="**/hellows/**"> <classpath refid="classpath"/> </javac> </target> <target name="create-war"> <war warfile="${build.war.home} /${ant.project.name}.war" webxml="config/web.xml"> <webinf dir="${basedir}/config" includes="sun-jaxws.xml"/> <zipfileset dir="${basedir}/config" includes="*.wsdl, *.xsd" prefix="WEB-INF/wsdl"/> <classes dir="${build.classes.home}" includes="**/*.class"/> </war> </target> Apache Ant build file, creatingclient, running<target name="generate-client"> <wsimport debug="true" verbose="${verbose}" destdir="${build.classes.home}" package="hellowsclient" clientjar="HelloWSServiceClient.jar" wsdl="http://localhost:8080/clientjar/hellows?wsdl"> </wsimport> </target> <target name="client" depends="generate-client"> <javac fork="true" srcdir="${basedir}/src" destdir="${build.classes.home}" includes="/hellowsclient/**"> <classpath> <path refid="classpath"/> <pathelement location="${build.classes.home} /HelloWSServiceClient.jar"/> </classpath> </javac> </target> <target name="run"> <java fork="true" classname="hellowsclient.HelloWSClient"> <classpath> <path refid="classpath"/> <pathelement location="${build.classes.home}"/> <pathelement location="${build.classes.home} /HelloWSServiceClient.jar"/> <pathelement location="${basedir}/config"/> </classpath> <jvmarg value="-Dcom.sun.xml.ws.transport. http.client.HttpTransportPipe.dump=${log}"/> </java> </target> <target name="server" depends="setup"> <antcall target="clean"/> <antcall target="build-server-wsdl"/> <antcall target="create-war"/> <antcall target="deploy"/> </target> </project>
The directory structure of the web service project is shown in the following screenshot: