Creating an Apache Ant build file

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:

Creating an Apache Ant build file

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:

Creating an Apache Ant build file

In Name and Location, specify the File Name as build, Project as HelloWS, and click on Finish:

Creating an Apache Ant build file

A build.xml file gets added to the HelloWS folder:

Creating an Apache Ant build file

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

java.home

C:/Program Files/Java/jdk1.7.0

modules.home

C:/glassfish3/glassfish/modules

as.home

C:/glassfish3/glassfish

build.home

${basedir}/build

build.war.home

${build.home}/war

build.classes.home

${build.home}/classes

domain

domain1

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:

  1. Build the web service WSDL to generate web service artifacts using the wsimport task.
  2. Compile the web service implementation class.
  3. Create a WAR file from the web service classes and deployment descriptors.
  4. Generate and package web service portable artifacts including the WSDLs into a JAR file using the new -clientjar option of wsimport.
  5. Compile the client class HelloWSClient.java.
  6. Run the web service client.

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 hellows directory and runs the wsimport task to generate web service artifacts.

create-war

Creates a WAR file from the compiled classes and includes the sun-jaxws.xml file and the WSDL.

generate-client

Generates the client JAR using the wsimport's -clientjar option.

client

Compiles the client class.

run

Runs the client class with the client JAR generated using the -clientjar option in the classpath.

deploy

Copies the web service WAR file to the GlassFish autodeploy directory.

server

Invokes the clean, build-server-wsdl, create-war, and deploy targets in the specified order.

clean

Deletes the build directory and its sub-directories.

The build.xml file is used to perform the following tasks:

  • Building and deploying the service
  • Building the client
  • Running the client

Building and deploying the service

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>

Building the client

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.

Running the client

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:

Running the client
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset