Ravish Mody

Here we are going to see how we can change the “<context-param>”  child tag values using plan.xml file. Many times we see that the values are not getting changed or reflected by plan.xml file updates even if every step looks correct.

NOTE: The plan.xml feature works on Xpath and which is applicable on the XSD based xml files. So it would not work for DTD based web.xml file.  So make sure that you use the weblogic.DDConverter utility to convert the web.xml to use the latest XSD based deployment descriptors. To get more information on it do have a look at this post How to use the weblogic.DDConverter tool ?

Steps To change the context-param values of web.xml using plan.xml

Step1).  Create a web application  in some location like: (C:/DemoPlan/SampleWebApp)

Step2).  Now create a “web.xml” file like following inside “C:/DemoPlan/SampleWebApp/WEB-INF” directory:

<!---------------- DO NOT use the DTDs inside your XML file if you want to use plan.xml------------------
<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <context-param id="one">
        <param-name>TokenGroup</param-name>
        <param-value>oldvalue</param-value>
    </context-param>
</web-app>
-----------------------Please ignore the above web.xml content -------------------------------------------->

<?xml version='1.0' encoding='WINDOWS-1252'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <context-param id="one">
    <param-name>TokenGroup</param-name>
    <param-value>oldvalue</param-value>
  </context-param>
</web-app>

Step3).  Now we need to create a “index.jsp” inside “C:/DemoPlan/SampleWebApp” like following to read the context-param values defined inside the “web.xml” file.

<html>
<head><title>Hi</title><head>
<body bgcolor=maroon text=white>

<%
     String param = application.getInitParameter("TokenGroup");
     out.println("<h3>TokenGroup : "+param);
     System.out.println("ntTokenGroup : "+param);
%>

</body>
</html>

Step4).  Now write the following kind of “plan.xml” file inside any directory like “C:/DemoPlan/”
NOTE: you need to make sure that the  <config-root>Should contain a valid directory path which contains the plan.xml file.</config-root>

<?xml version='1.0' encoding='UTF-8'?>
<deployment-plan xmlns="http://www.bea.com/ns/weblogic/deployment-plan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/deployment-plan http://www.bea.com/ns/weblogic/deployment-plan/1.0/deployment-plan.xsd" global-variables="false">
  <application-name>SimpleWebApp</application-name>

  <variable-definition>
      <variable>
          <name>newVariable</name>
          <value>Key123456</value>
      </variable>
  </variable-definition>

  <module-override>
    <module-name>SimpleWebApp</module-name>
    <module-type>war</module-type>

    <module-descriptor external="false">
      <root-element>web-app</root-element>
      <uri>WEB-INF/web.xml</uri>
      <variable-assignment>
         <name>newVariable</name>
         <xpath>/web-app/context-param/[param-name="TokenGroup"]/param-value</xpath>
         <operation>add</operation>
      </variable-assignment>
    </module-descriptor>

  </module-override>
  <config-root>C:/DemoPlan/</config-root>
</deployment-plan>

Step5). Now just to simplify the deployment and un-deployment task we can write the build.xml file  to make the things automated.

<project name="webservices-hello_world" default="deploy">
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="10.10.10.10" />
<property name="wls.port" value="7001" />
<property name="admin.server.name" value="AdminServer" />
<property name="deploy.target" value="AdminServer" />

<property name="deploy.name" value="SimpleWebApp" />
<property name="deploy.source" value="SimpleWebApp" />
<property name="plan.xml.path" value="C:/DemoPlan//plan.xml" />

<!-- Setting TaskDefinition -->
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy">
<classpath>
<pathelement location="C:/bea103/wlserver_10.3/server/lib/weblogic.jar"/>
</classpath>
</taskdef>

<!-- Deploying Applications  -->
<target name="deploy">
<wldeploy action="deploy"
          name="${deploy.name}"
          source="${deploy.source}"
          user="${wls.username}"
          nostage="true"
          password="${wls.password}"
          verbose="true"
          adminurl="t3://${wls.hostname}:${wls.port}" targets="${deploy.target}" />
</target>

<target name="redeployWithPlan">
<wldeploy action="redeploy"
          name="${deploy.name}"
          plan="${plan.xml.path}"
          user="${wls.username}"
          password="${wls.password}"
          verbose="true"
          adminurl="t3://${wls.hostname}:${wls.port}" targets="${deploy.target}" />
</target>

<!-- Redeploying Applications  -->
<target name="redeploy">
<wldeploy action="redeploy"
          name="${deploy.name}"
          user="${wls.username}"
          password="${wls.password}"
          verbose="true"
          adminurl="t3://${wls.hostname}:${wls.port}" targets="${deploy.target}" />
</target>

<!-- Uneploying Applications  -->
<target name="undeploy">
<wldeploy action="undeploy"
          name="${deploy.name}"
          failonerror="false"
          user="${wls.username}"
          password="${wls.password}"
          verbose="true"
          adminurl="t3://${wls.hostname}:${wls.port}" targets="${deploy.target}" />
</target>
</project>

Step6). Now deploy your application after running the setWLSEnv.cmd in the command prompt like following:

ant deploy

Step7). Now access the application like http://hostName:port/SampleWebApp/index.jsp   you will notice that the  page displays the value of TokenGroup as “oldvalue” which is defined in the web.xml .

Step8). Now again redeploy your application using the plan.xml file  which is specified in the  “build.xml” file as following:

ant redeployWithPlan

Step9).  Now as soon as you will hit your application “http://hostName:port/SampleWebApp/index.jsp” you will see that the page displays the value of TokenGroup as “Key123456

Thanks
Ravish Mody

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.