mardi 9 août 2011

Ant: Deploy an application over a Weblogic instance or cluster

Some ant stuff to help deployment of an application over a veblogic cluster or instance :
<project name="deployment tool">
    <!-- ********************************************************************************** -->
    <!-- Imports the properties files used for correct deployment                           -->
    <!-- ********************************************************************************** -->
    <property file="deploy.properties" />

    <!-- ********************************************************************************** -->
    <!-- Taks definitions                                                                   -->
    <!-- ********************************************************************************** -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${ant.lib.dir}/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy">
        <classpath>
            <pathelement path="${commonlibs.dir}/Storage/wlfullclient.jar" />
        </classpath>
    </taskdef>


    <!-- ********************************************************************************** -->
    <!-- deploying/undeploying module in application server                                 -->
    <!-- ********************************************************************************** -->
    <macrodef name="deploy-module">
       <attribute name="file" default="NOT_SET"/>
       <attribute name="wl.name" default="NOT_SET"/>
       <attribute name="wl.username" default="weblogic"/>
       <attribute name="wl.password" default="testpwd"/>
       <attribute name="wl.adminurl" default="t3://localhost:7001"/>
       <attribute name="wl.targets" default="NOT_SET"/>
       <sequential>
           <!-- Check that the admin server is running before deployment -->
           <property name="weblogic.console" value="http://${server-host}:${server-adminPort}/console"/>
           <waitfor maxwait="2" maxwaitunit="minute" checkevery="100" checkeveryunit="millisecond">
               <http url="${weblogic.console}" />
           </waitfor>
           <if>
               <not>
                   <http url="${weblogic.console}" />
               </not>
               <then>
                   <fail message="Admin Server on http://${server-host}:${server-adminPort} is not in running state. Unable to deploy ${final.ear.name}"/>
               </then>
               <else>
                   <echo message="_____Admin Server on http://${server-host}:${server-adminPort} is running. Starting deployment of ${final.ear.name}"/>
               </else>
           </if>
           <!-- starting the deployment -->
           <echo>_____Deploying file @{file} to @{wl.adminurl} on targets @{wl.targets} </echo>
           <if>
               <equals arg1="${server-host}" arg2="localhost"/>
               <then>
                   <property name="wl.upload" value="false"/>
               </then>
               <else>
                   <property name="wl.upload" value="true"/>
                   <echo>_____Uploading file @{file} to @{wl.adminurl} </echo>
               </else>
           </if>
           <if>
               <equals  arg1="@{wl.targets}" arg2="NOT_SET"/>
               <then>
                   <wldeploy action="deploy" source="@{file}" name="@{wl.name}" user="@{wl.username}" password="@{wl.password}" adminurl="@{wl.adminurl}" verbose="true" debug="true" upload="${wl.upload}"/>
               </then>
               <else>
                   <wldeploy action="deploy" source="@{file}" name="@{wl.name}" user="@{wl.username}" password="@{wl.password}" adminurl="@{wl.adminurl}" targets="@{wl.targets}" verbose="true" debug="true" upload="${wl.upload}"/>
               </else>
           </if>
       </sequential>
    </macrodef>

    <macrodef name="undeploy-module">
       <attribute name="wl.name" default="NOT_SET"/>
       <attribute name="wl.username" default="weblogic"/>
       <attribute name="wl.password" default="testpwd"/>
       <attribute name="wl.adminurl" default="t3://localhost:7001"/>
       <attribute name="wl.targets" default="NOT_SET"/>
       <sequential>
           <echo>_____Undeploying module @{wl.name} to @{wl.adminurl} from targets @{wl.targets} </echo>
           <if>
               <equals  arg1="@{wl.targets}" arg2="NOT_SET"/>
               <then>
                   <wldeploy action="undeploy" name="@{wl.name}" user="@{wl.username}" password="@{wl.password}" adminurl="@{wl.adminurl}" verbose="true" debug="true" failonerror="false"/>
               </then>
               <else>
                   <wldeploy action="undeploy" name="@{wl.name}" user="@{wl.username}" password="@{wl.password}" adminurl="@{wl.adminurl}" targets="@{wl.targets}" verbose="true" debug="true" failonerror="false"/>
               </else>
           </if>
       </sequential>
    </macrodef>

    <!-- ********************************************************************************** -->
    <!-- deploying/undeploying ear in application server                                    -->
    <!-- ********************************************************************************** -->
    <target name="deploy-ear">
        <!-- deploy now the module -->
        <deploy-module file="${final.module.name}"
                       wl.name="${project.name}"
                       wl.username="${serverWL-adminuser}"
                       wl.password="${serverWL-adminpasswd}"
                       wl.adminurl="t3://${server-host}:${server-adminPort}"
                       wl.targets="${cluster-name}"
                />
    </target>

    <target name="undeploy-ear">
        <undeploy-module wl.name="${project.name}"
                         wl.username="${serverWL-adminuser}"
                         wl.password="${serverWL-adminpasswd}"
                         wl.adminurl="t3://${server-host}:${server-adminPort}"
                         wl.targets="${cluster-name}"
                />
    </target>

</project>

and use a property file like this:
ant.lib.dir=D:/tools/apache-ant/lib

# Weblogic home dir and domain location
# Variable only used to start and stop the server when localhost is used
# in server location
weblogic.home.dir=D:/weblogic.10.3.4
weblogic.domain.dir=D:/weblogic.10.3.4/user_projects/domains/YuuWaa

# The command to run for starting and stoping web logic
start-admin-server=startWebLogic.cmd
stop-admin-server=stopWebLogic.cmd
start-managed-server=startManagedWebLogic.cmd
stop-managed-server=stopManagedWebLogic.cmd

# Cluste name where to deploy
cluster-name=

# host and port of the listening admin console
server-host=localhost
server-adminPort=7001

# User/Password used when connecting to the admin console of WLS
serverWL-adminuser=weblogic
serverWL-adminpasswd=testpwd

final.module.name=testEjb.ear