Ant script to deploy ear in websphere

IBM WebSphere Application Server

Deploying an application using ws_ant

 

Before we can run a ws_ant automated application deployment, we need to create an XML file which will contain the ws_ant tasks we wish to run to deploy the application. Traditionally, Ant-based utilities look for a default XML file called build.xml. However, any XML filename can be used as long as you pass the name of the file on the command line when you run ws_ant.

—> We could use the name build.xml for the XML file to keep in line with the common use of Ant, but by using a different file we immediately know what the script is used for by just looking at its name.

Before we create our first script you need to reference a few folders from within the scripts. The folders you will need to create or reference for the scripts to work are explained as follows:

<scripts_home> :- The root location on your server where you will locate all your scripts.
(In the examples, I have used /root/WAS8TECHPASTE on Linux as my script location)

<deploy_home>  :- The root location where you will store all EAR files.
(In the examples, I have used /root/WAS8TECHPASTE/deploy on Linux as my EAR file location).

<was_root>  :- The root of your WAS installation.

<was_profile_root> :- The root of your Application Server profile.

The steps to create the script are as follows:

1. Create a file called deployapp.xml in a folder called <scripts_home>/ws_ant.

2. Copy the following code into deployapp.xml, ensuring you replace appropriate paths variables and save the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HR Lister Application Depoyment" default="buildall" basedir=".">
<!-- global properties -->
<property name="hostName" value="localhost" />
<property name="connType" value="SOAP" />
<property name="port" value="8880" />
<property name="userId" value="wasadmin" />
<property name="password" value="wasadmin" />
<property name="deployEar.dir" value="<deploy_home>" />
<property name="deployEar" value="HRListerEAR.ear" />
<property name="wasHome.dir" value="<was_root>" />
<!-- mbean declaration" -->
<taskdef name="wasInstallApp" classname="com.ibm.websphere.ant.tasks.InstallApplication" />
<!-- installation Target-->
<target name="installEar">
<echo message="Deployable EAR File found at: ${deployEar.dir}/${deployEar}" />
<wasInstallApp ear="${deployEar.dir}/${deployEar}"
wasHome="${wasHome.dir}"
conntype="${connType}"
port="${port}"
host="${hostName}"
user="${userId}"
password="${password}" />
</target>
<target name="build-all" depends="installEar">
<!--Main Target-->
</target>
</project>

3. To run the ws_ant script, navigate to the <was_root>/bin folder and type the following command:

°° For Linux:
./ws_ant.sh -f <scripts_home>/ws_ant/deployapp.xml

°° For Windows:
ws_ant.bat -f <scripts_home>\ws_ant\deployapp.xml

4. The result of running the deployapp.xml ws_ant script will be displayed in stdout (the shell scripts console). A sample of the output you would expect to see is as follows:

installEar:
[echo] Deployable EAR File found at: /root/WAS8TECHPASTE/deploy/HRListerEAR.ear
[wasInstallApp] Installing Application [/root/WAS8TECHPASTE/deploy/HRListerEAR.ear]...
[wsadmin] WASX7209I: Connected to process "server01" on node node01 using SOAP connector; The type of process is:UnManagedProcess
[wsadmin] ADMA5016I: Installation of HRListerEAR started.
[wsadmin] ADMA5058I: Application and module versions are validated with versions of deployment targets.
[wsadmin] ADMA5005I: The application HRListerEAR is configured in the WebSphere Application Server repository.
[wsadmin] ADMA5053I: The library references for the installed optional package are created.
[wsadmin] ADMA5005I: The application HRListerEAR is configured in the WebSphere Application Server repository.
[wsadmin] ADMA5001I: The application binaries are saved in /var/apps/was8/profiles/appsrv01/wstemp/Script12edf1052fc/workspace/cells/s15418557Node01Cell/applications/HRListerEAR.ear/HRListerEAR.ear
[wsadmin] ADMA5005I: The application HRListerEAR is configured in the WebSphere Application Server repository.
[wsadmin] SECJ0400I: Successfully updated the application  HRListerEAR with the appContextIDForSecurity information.
[wsadmin] ADMA5005I: The application HRListerEAR is configured in the WebSphere Application Server repository.
[wsadmin] ADMA5113I: Activation plan created successfully.
[wsadmin] ADMA5011I: The cleanup of the temp directory for application HRListerEAR is complete.
[wsadmin] ADMA5013I: Application HRListerEAR installed successfully.
[wasInstallApp] Installed Application [/root/WAS8TECHPASTE/deploy/HRListerEAR.ear]

build-all:
BUILD SUCCESSFUL
Total time: 17 seconds

 

Managing an application using ws_ant

 

1. Create a file called manageapp.xml in the same folder you created earlier, for example, <scripts_home>/was_ant/manageapp.xml.

2. Copy the following code into the manageapp.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Manage HR Lister Application Deployment" default="build-all" basedir=".">
<!-- global properties -->
<property name="hostName" value="localhost" />
<property name="connType" value="SOAP" />
<property name="port" value="8880" />
<property name="userId" value="wasadmin" />
<property name="password" value="wasadmin" />
<property name="appName" value="HRLister" />
<property name="deployEar.dir" value="="<deploy_home>" />
<property name="deployEar" value="HRListerEAR.ear" />
<property name="wasHome.dir" value="<was_root>" />
<!-- mbean declarations" -->
<taskdef name="wsUninstallApp" classname="com.ibm.websphere.
ant.tasks.UninstallApplication" />
<taskdef name="wsInstallApp" classname="com.ibm.websphere.ant.
tasks.InstallApplication" />
<taskdef name="wsListApplications" classname="com.ibm.
websphere.ant.tasks.ListApplications" />
<!-- List Target-->
<target name="listApplications">
<wsListApplications
wasHome="${wasHome.dir}"
conntype="${connType}"
port="${port}"
host="${hostName}"
user="${userId}"
password="${password}" />
</target>
<!-- Uninstall Target-->
<target name="uninstallEAR">
<wsUninstallApp application="${appName}"
wasHome="${wasHome.dir}"
conntype="${connType}"
port="${port}"
host="${hostName}"
user="${userId}"
password="${password}" />
</target>
<!-- installation Target-->
<target name="installEAR">
<echo message="Deployable EAR File found at: ${deployEar.dir}/${deployEar}" />
<wsInstallApp ear="${deployEar.dir}/${deployEar}"
options="-appname ${appName}"
wasHome="${wasHome.dir}"
conntype="${connType}"
port="${port}"
host="${hostName}"
user="${userId}"
password="${password}" />
</target>
<target name="build-all" depends="listApplications,uninstallEAR, listApplications, installEAR, listApplications">
<!--Main Target-->
</target>
</project>

Using the following table, you can see the syntax of the different types of command lines required to run the individual targets:

listApplications

For Linux:
<was_root>/bin/ws_ant.sh listApplications -f <scripts_home>/ws_ant/manageapp.xml

For Windows:
<was_root>\bin\ws_ant.bat listApplications -f <scripts_home>\ws_ant\manageapp.xml

uninstallEAR

For Linux:
<was_root>/bin/ws_ant.sh uninstallEAR -f <scripts_home>/ws_ant/manageapp.xml

For Windows:
<was_root>\bin\ws_ant.bat uninstallEAR -f <scripts_home>\ws_ant\manageapp.xml

installEAR

For Linux:
<was_root>/bin/ws_ant.sh installEAR -f <scripts_home>/ws_ant/manageapp.xml

For Windows:
<was_root>\bin\ws_ant.sh installEAR -f <scripts_home>\ws_ant\manageapp.xml

<No target specified> (Runs all targets)

For Linux:
<was_root>/bin/ws_ant.sh install -f <scripts_home>/ws_ant/manageapp.xml

For Windows:
<was_root>\bin\ws_ant.sh install -f <scripts_home>\ws_ant\manageapp.xml

3. if you choose to run the manageapp.xml file with no specific target mentioned, the build-all target will run. The build-all target calls named targets in a particular order.

<target name="build-all" depends="listApplications, uninstallEAR, listApplications, installEAR, listApplications">
<!--Main Target-->
</target>

4. Since the manageapp.xml file has a project declaration, which specifies the default target as build-all, the build-all target will be called if no target name is specified on the command line. This is shown as follows:

<project name="Manage Application Deployment" default="build-all" basedir=".">

IBM maintains that ws_ant is not suited for production system configurations, though this does not stop anyone from using Ant as such. A better tool for administrative scripting in WAS is the wsadmin tool. The reason is that administration scripting using Jython is far more powerful than ws_ant. It is interesting to note that ws_ant is in fact an Ant wrapper which calls some wsadmin commands internally.

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

1 Response

  1. Dexter says:

    Is their any way to check if the application is deployed already and then invoke the Corresponding Target(Deploy/Update).

Leave a Reply