Automate Web services with Ant and WebSphere

Use WebSphere Ant tasks to develop and test Web services


Ant is a widely used development tool for building and automating processes using XML files. By using some of the core Ant tasks along with WebSphere's custom Ant tasks you can develop a Web service application and install it into WebSphere Application Server. In this article, you'll learn how to create and install a Web service-enabled Enterprise Java™ Bean (EJB) by supplying a service endpoint interface, a few standard Java 2 Enterprise Edition (J2EE) deployment descriptors, and an Ant build file.


This article assumes that you have WebSphere Application Server installed. The listings and sample code included with this article were developed using Application Server V6.0.2. WebSphere includes a version of Ant in the default install. You can use it by executing the ws_ant.bat/sh file in the WebSphere bin directory (C:\WebSphere\AppServer\bin\ws_ant.bat).

Why use Ant?

The advantages of using Ant to build a Web service application include:

  • Repeatability - The ability to change a few settings or code in an application and regenerate the entire application is invaluable. Using an Ant build file, the steps to create the application are the same each time, which improves productivity and consistency.
  • Automatability - An Ant XML build file can run without human interaction during nightly or scheduled builds. The Ant process can run in build environments without the need for a terminal.
  • Platform independence - Ant runs on all major platforms from Windows® to OS/390.
  • Availability - WebSphere contains Ant when installed, and also includes some custom Ant tasks for automating tasks in WebSphere.
  • Skills - A wealth of articles and books exist on using Ant. Its pervasive use in the Java community means that many developers now have the skills to develop Ant build files.

Create a Web service with Java2WSDL

In this example, the starting point for creating the Web service is a service endpoint interface (SEI) from which Java2WSDL generates a WSDL file. The WSDL file is a key piece in building a Web service. After its creation, the WSDL2Java tool can process the WSDL file to generate many of the artifacts to create a working Web service application.

The SEI defined in Listing 1 contains a simple helloWorld method that sends and receives a String. The JSR-101 specification defines the rules for creating a SEI.

Listing 1. Service Endpoint Interface
  package example;

  interface WSExample extends java.rmi.Remote {
      String helloWorld(String message);


The first step in creating an Ant build file is to define which custom WebSphere Ant tasks the build process will use. You can find the API for all WebSphere Ant tasks in the the WebSphere Information Center. You may find it helpful to view the Ant documentation while working on Ant build files to determine how to use the core Ant tasks that come with each version of Ant.

In Listing 2, the build file contains many <taskdef> statements, a core Ant element. These statements enable Ant to use the custom WebSphere Ant tasks that we'll use throughout the rest of this example.

Listing 2. Define tasks
  <path id="lib.path">
	<pathelement location="${WSAS.home}/lib/*.jar"/>

  <taskdef name="wsdl2java"
    <classpath refid="lib.path"/>

  <taskdef name="java2wsdl"
    <classpath refid="lib.path"/>

  <taskdef name="endptenabler"
    <classpath refid="lib.path"/>

  <taskdef name="wsdeploy"
    <classpath refid="lib.path"/>

  <taskdef name="wsejbdeploy"
     <classpath refid="lib.path"/>

  <taskdef name="installapp"
    <classpath refid="lib.path"/>

The first execution statement in the Ant build file compiles the SEI as shown in Listing 3. With the compiled SEI class in the classpath, the custom WebSphere Java2WSDL Ant task can generate a WSDL file. Note that you should manually verify the WSDL file generated by Java2WSDL at least once after its initial creation. In some cases, the WSDL file generated may not contain the exact XML schema types desired due to mapping rules between Java and XML. For a description of the XML schema mappings between Java and XML, view the JSR-101 specification.

The Java2WSDL Ant task contains a number of options to allow you to customize the generated WSDL file. Check out the Javadoc for more specifics about each attribute and its valid values.

Listing 3. Java2WSDL Ant task
  <!-- Compile the SEI class -->    
  <javac srcdir="${WSAS.home}/bin"
    <classpath refid="lib.path"/>
    <include name=""/>

  <!-- With the SEI class in the classpath, run the java2wsdl task -->
	<java2wsdl output="${WSAS.home}/bin/tmp/META-INF/wsdl/WSExample.wsdl"
             className= "example.WSExample"
    <mapping namespace="http://example" package="example"/>

Use WSDL2Java

Now that we've created a WSDL file using the Java2WSDL Ant task, we can start generating the remaining Web service files. The WSDL2Java Ant task creates a number of necessary files for developing a complete Web service enabled application.

These files include both XML files and Java files. The XML files generated include:

  • webservices.xml
  • mapping_file.xml

The Java files include:

  • Service Endpoint Interface
  • EJB home
  • EJB remote
  • EJB implementation class

As with the Java2WSDL Ant task, the WSDL2Java Ant task has many attributes you can use to customize the generation of the Web services files. View the Javadoc for WSDL2Java for more specifics.

In Listing 4, the role and container attributes are develop-server and EJB, respectively. These attributes signal to the WSDL2Java Ant task to generate the server development files for an EJB.

Listing 4. WSDL2Java Ant task
  <wsdl2java url="${WSAS.home}/bin/WSExample.wsdl"

You must create two standard J2EE deployment descriptors and an EJB implementation class outside of the Ant build process:

  • ejb-jar.xml - packaged in the EJB JAR file
  • application.xml - packaged in the EAR file
  • - EJB implementation class

Although the WSDL2Java Ant task generates a template EJB implementation class, in our example we'll replace it with one that has some customized business logic.

In the webservices.xml file created by WSDL2Java, you must change the element, <ejb-link>, to match the <ejb-name> in the ejb-jar.xml. Listing 5 shows the ejb-jar.xml file created outside of the build process, and the webservices.xml file created by WSDL2Java.

Listing 5. ejb-jar.xml and webservices.xml
    The ejb-jar.xml is created manually outside of the automated
    build process and packaged in the EJB jar.
  <?xml version="1.0" encoding="UTF-8"?>
  <ejb-jar id="ejb-jar_ID" version="2.1"
      <session id="Session_1">
    The WSDL2Java Ant task generates this webservices.xml file.
  <?xml version="1.0" encoding="UTF-8"?>
  <webservices xmlns=""
              <wsdl-port xmlns:pfx="http://example">pfx:WSExample</wsdl-port>
                  <ejb-link>??SET THIS TO ejb-name ELEMENT OF ejb-jar.xml??</ejb-link>

You must replace the <ejb-link> element with the actual <ejb-name> supplied in the ejb-jar.xml file shown in Listing 5. The core Ant replace task is perfect for this task, as shown in Listing 6.

Listing 6. Replace <ejb-link>
  <replace file="${WSAS.home}/bin/tmp/META-INF/webservices.xml"
           token="??SET THIS TO ejb-name ELEMENT OF ejb-jar.xml??"

Build the application

After WSDL2Java runs, the WSDL2Java generated classes need to be compiled, as well as any additional or customized classes needed in the Web service enabled EJB application. In Listing 7, the javac core Ant task compiles the classes.

Listing 7. Compile classes
  <javac srcdir="${WSAS.home}/bin/tmp/**/*.java"
    <classpath refid="lib.path"/>
    <include name=""/>

Once all the classes have compiled, the Ant build file creates a standard J2EE JAR file to contain all the compiled Java classes and XML files. Listing 8 shows the core jar Ant task used for completing this step.

Listing 8. Create JAR
  <jar jarfile="${WSAS.home}/bin/tmp/WSExample.jar">
    <fileset dir="${WSAS.home}/bin/tmp">
      <include name="**/*.class"/> 
    <zipfileset dir="${WSAS.home}/bin/tmp/META-INF" prefix="META-INF">
      <include name="**/*"/>

After running the jar task, the following files exist in the EJB JAR:

  • META-INF/ejb-jar.xml
  • META-INF/ibm-webservices-bnd.xmi
  • META-INF/ibm-webservices-ext.xmi
  • META-INF/webservices.xml
  • META-INF/WSExample_mapping.xml
  • META-INF/wsdl/WSExample.wsdl
  • example/WSExample.class
  • example/WSExampleHome.class
  • example/WSExampleSoapBindingImpl.class
  • example/WSExample_RI.class

Listing 9 shows the completion of the Web service application by adding the EJB JAR file created in Listing 8 to a standard J2EE EAR file. Ant provides a core ear task to perform this operation.

Listing 9. Create EAR
    <ear earfile="${WSAS.home}/bin/tmp/WSExample.ear"
      <fileset dir="${WSAS.home}/bin/tmp">
        <include name="WSExample.jar"/>

After execution of the ear task, the resulting EAR contains these files:

  • META-INF/application.xml
  • WSExample.jar

Before you install

In order for EJBs enabled as Web services to be reachable through the HTTP protocol, they must have an HTTP router in the application. WebSphere's endptenabler Ant task adds this router to the EAR file. As shown in Listing 10, the property elements added to the endptenabler task allow for customization of the router modules that get created. For complete documentation of the options, view the JavaDoc.

Listing 10. EndpointEnabler
  <endptenabler earfile="${WSAS.home}/bin/tmp/WSExample.ear">
    <property key="verbose" value="false"/>
    <property key="WSExample.transports" value="http"/>
    <property key="WSExample.http.rounterModuleName" value="WSExample_HTTPRouter.war"/>
    <property key="WSExample.http.contextRoot" value="wsexample"/>

The last step prior to installing this application into WebSphere Applicaton Server is to run wsejbdeploy and wsdeploy tasks. wsejbdeploy creates all the classes necessary for communicating with an EJB. Listing 11 shows an example of how to use the wsejbdeploy task.

Listing 11. EJBDeploy Task
  <wsejbdeploy inputJar="${WSAS.home}/bin/tmp/WSExample.ear"

wsdeploy generates any WebSphere-specific Web services artifacts necessary for communicating with the application. The example invocation of wsdeploy validates and produces special classes such as WebSphere-specific serializers and deserializers.

Listing 12. WSDeploy Task
  <wsdeploy classPath=""

Install the application

You can now install the the application into WebSphere using the install Ant task, as shown in Listing 13. The example assumes that WebSphere is not running, because it uses a conntype of NONE.

Listing 13. Install the application
  <installapp wasHome="${WSAS.home}"

Run the application

To run the sample application, download the sample code. Unzip it in C:\WebSphere\AppServer\bin and execute the folllowing command:

"C:\WebSphere\AppServer\bin\ws_ant.bat -DWSAS.home=C:\WebSphere\AppServer -f C:\WebSphere\AppServer\bin\build.xml


In this article, you've learned an automated way to build and install a Web service enabled application. WebSphere provides a rich set of Ant tasks to perform many operation. You can combine these tasks to create processes that are repeatable, less error-prone, and faster.

Downloadable resources

Related topics


Sign in or register to add and subscribe to comments.

Zone=WebSphere, SOA and web services
ArticleTitle=Automate Web services with Ant and WebSphere