Example of a build.xml for an EAR that deploys in WebSphere 6

ggasp picture ggasp · Aug 6, 2008 · Viewed 23.2k times · Source

I'm trying to convince my providers to use ANT instead of Rational Application Development so anyone can recompile, recheck, redeploy the solution anyplace, anytime, anyhow. :P

I started a build.xml for a project that generates a JAR file but stopped there and I need real examples to compare notes. My good friends! I don't have anyone close to chat about this!

This is my build.xml so far.

(*) I edited my question based in the suggestion of to use pastebin.ca

Answer

McDowell picture McDowell · Aug 7, 2008

My Environment: Fedora 8; WAS 6.1 (as installed with Rational Application Developer 7)

The documentation is very poor in this area and there is a dearth of practical examples.

Using the WebSphere Application Server (WAS) Ant tasks

To run as described here, you need to run them from your server profile bin directory using the ws_ant.sh or ws_ant.bat commands.

<?xml version="1.0"?>
<project name="project" default="wasListApps" basedir=".">
    <description>
        Script for listing installed apps.
        Example run from:
        /opt/IBM/SDP70/runtimes/base_v61/profiles/AppSrv01/bin
    </description>

    <property name="was_home"
        value="/opt/IBM/SDP70/runtimes/base_v61/">
    </property>
    <path id="was.runtime">
        <fileset dir="${was_home}/lib">
            <include name="**/*.jar" />
        </fileset>
        <fileset dir="${was_home}/plugins">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <property name="was_cp" value="${toString:was.runtime}"></property>
    <property environment="env"></property>

    <target name="wasListApps">
        <taskdef name="wsListApp"
            classname="com.ibm.websphere.ant.tasks.ListApplications"
            classpath="${was_cp}">
        </taskdef>
        <wsListApp wasHome="${was_home}" />
    </target>

</project>

Command:

./ws_ant.sh -buildfile ~/IBM/rationalsdp7.0/workspace/mywebappDeploy/applist.xml

A Deployment Script

<?xml version="1.0"?>
<project name="project" default="default" basedir=".">
<description>
Build/Deploy an EAR to WebSphere Application Server 6.1
</description>

    <property name="was_home" value="/opt/IBM/SDP70/runtimes/base_v61/" />
    <path id="was.runtime">
        <fileset dir="${was_home}/lib">
            <include name="**/*.jar" />
        </fileset>
        <fileset dir="${was_home}/plugins">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <property name="was_cp" value="${toString:was.runtime}" />
    <property environment="env" />
    <property name="ear" value="${env.HOME}/IBM/rationalsdp7.0/workspace/mywebappDeploy/mywebappEAR.ear" />

    <target name="default" depends="deployEar">
    </target>

    <target name="generateWar" depends="compileWarClasses">
        <jar destfile="mywebapp.war">
            <fileset dir="../mywebapp/WebContent">
            </fileset>
        </jar>
    </target>

    <target name="compileWarClasses">
        <echo message="was_cp=${was_cp}" />
        <javac srcdir="../mywebapp/src" destdir="../mywebapp/WebContent/WEB-INF/classes" classpath="${was_cp}">
        </javac>
    </target>

    <target name="generateEar" depends="generateWar">
        <mkdir dir="./earbin/META-INF"/>
        <move file="mywebapp.war" todir="./earbin" />
        <copy file="../mywebappEAR/META-INF/application.xml" todir="./earbin/META-INF" />
        <jar destfile="${ear}">
            <fileset dir="./earbin" />
        </jar>
    </target>

    <!-- http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.javadoc.doc/public_html/api/com/ibm/websphere/ant/tasks/package-summary.html -->
    <target name="deployEar" depends="generateEar">
        <taskdef name="wsInstallApp" classname="com.ibm.websphere.ant.tasks.InstallApplication" classpath="${was_cp}"/>
        <wsInstallApp ear="${ear}" 
            failonerror="true" 
            debug="true" 
            taskname=""
            washome="${was_home}" />
    </target>

</project>

Notes:

  • You can only run this once! You cannot install if the app name is in use - see other tasks like wsUninstallApp
  • It probably won't start the app either
  • You need to run this on the server and the script is quite fragile

Alternatives

I would probably use Java Management Extensions (JMX). You could write a file-upload servlet that accepts an EAR and uses the deployment MBeans to deploy the EAR on the server. You would just POST the file over HTTP. This would avoid any WAS API dependencies on your dev/build machine and could be independent of any one project.