Affichage des articles dont le libellé est Ant. Afficher tous les articles
Affichage des articles dont le libellé est Ant. Afficher tous les articles

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

Ant : Starting/Stoping a weblogic instance or cluster

Here some ant script used to start/stop a weblogic instance or cluster
<!-- ********************************************************************************** -->
    <!-- Taks definitions                                                                   -->
    <!-- ********************************************************************************** -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${ant.lib.dir}/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <!-- ********************************************************************************** -->
    <!-- startWeblogic                                                                      --v
    <!-- For remote access using ssh define the properties :                                -->
    <!--       server-user                                                                  -->
    <!--       server-passwd                                                                -->
    <!-- ********************************************************************************** -->
    <target name="start-weblogic" description="Start weblogic">
        <if>
         <equals arg1="${server-host}" arg2="localhost" />
         <then>
             <echo message="__________  Check Weblogic" />
             <if>
                 <available file="${weblogic.home.dir}/wlserver_10.3/server/lib/weblogic.jar"/>
                 <then>
                     <echo message="Weblogic server found" />
                 </then>
                 <else>
                     <fail message="No Weblogic server has been found" />
                 </else>
             </if>

             <echo message="__________ Check if libraries correctly installed" />

             <echo message="__________ Stop Weblogic server if started" />
             <antcall target="stop-weblogic" inheritrefs="true" />

             <echo message="__________ Start Weblogic" />

             <property name="weblogic.console" value="http://${server-host}:${server-adminPort}/console"/>
             <if>
                 <not>
                     <http url="${weblogic.console}" />
                 </not>
                 <then>
                     <echo message="Weblogic is not running" />
                     <echo message="Starting Weblogic server..." />
                     <forget>
                         <exec executable="${weblogic.domain.dir}/bin/${start-admin-server}" dir="${weblogic.domain.dir}/bin" />
                     </forget>
                     <waitfor maxwait="2" maxwaitunit="minute" checkevery="100" checkeveryunit="millisecond">
                         <http url="${weblogic.console}" />
                     </waitfor>
                     <echo message="Weblogic started" />
                 </then>
                 <else>
                     <echo message="Weblogic server is already running" />
                 </else>
             </if>
         </then>
         <else>
             <sshexec host="${server-host}" username="${server-user}" password="${server-passwd}" command="${weblogic.domain.dir}/bin/${start-managed-server}" trust="yes" failonerror="no" />
             <sleep seconds="60" />
         </else>
        </if>
	</target>

    <!-- ********************************************************************************** -->
    <!-- stopWeblogic                                                                       -->
    <!-- For remote access using ssh define the properties :                                -->
    <!--       server-user                                                                  -->
    <!--       server-passwd                                                                -->
    <!-- ********************************************************************************** -->
	<target name="stop-weblogic" description="Stop weblogic">
        <if>
           <equals arg1="${server-host}" arg2="localhost" />
           <then>
               <echo message="__________ Stop Weblogic" />
               <property name="weblogic.console" value="http://${server-host}:${server-adminPort}/console"/>

               <if>
                   <http url="${weblogic.console}" />
                   <then>
                       <echo message="Weblogic is running" />
                       <echo message="Stopping Weblogic server..." />
                       <forget>
                           <exec executable="${weblogic.domain.dir}/bin/${stop-admin-server}" dir="${weblogic.domain.dir}/bin" />
                       </forget>
                       <waitfor maxwait="2" maxwaitunit="minute" checkevery="100" checkeveryunit="millisecond">
                           <not>
                               <http url="${weblogic.console}" />
                           </not>
                       </waitfor>
                       <echo message="Weblogic stopped" />
                   </then>
                   <else>
                       <echo message="Weblogic server is already stopped" />
                   </else>
               </if>
           </then>
           <else>
		       <sshexec host="${server-host}" username="${server-user}" password="${server-passwd}" command="${weblogic.domain.dir}/bin/${stop-managed-server}" trust="yes" failonerror="no" />
		       <sleep seconds="20" />
           </else>
        </if>
	</target>


Define also some properties :
# 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