Getting the deployment plan to work might be sometimes a little bit tricky in Weblogic. Here’s my experience summary. Hopefully it will save some time to someone.
Generating deployment plan XML
- Set the environment
Linux:source <Install Path>/Middleware/wlserver_10.3/server/bin/setWLSEnv.sh
Windows:
<Install Path>\Middleware\wlserver_10.3\server\bin\setWLSEnv.cmd
- Generate the plan
java weblogic.PlanGenerator -all Application.ear
This will generate plan.xml file in the current directory.
What’s inside plan.xml
The plan.xml is basically divided in two main sections. Where you define variables and where you assign them.
Variable definition looks like this:
<variable> <name>variable_name</name> <value>variable_value</value> </variable>
Variable assignment:
<module-override> <module-name>Application.ear</module-name> <module-type>ear</module-type> <module-descriptor external="false"> <root-element>application</root-element> <uri>META-INF/application.xml</uri> <variable-assignment> <name>variable_name</name> <xpath>/path/in/xml/[element="value"]/element</xpath> <operation>add or replace</operation> </variable-assignment> </module-descriptor> </module-override>
In the generated plan.xml, you will have already some variables for current settings used in EAR’s generated and assigned with the right <xpath>
element set. If you set a value to a generated variable, you have to remove the xsi:nil="true"
attribute in the <value>
element. And in the assign section, you have to add <operation>
element with replace. If you create your own variable and want to add the value, you type add in the <operation>
element. External
attribute is saying whether the XML is (false) in the EAR/WAR or not (true).
Changing the context root
EAR with WAR and context root defined in application.xml
Application.xml
<?xml version = '1.0' encoding = 'windows-1252'?> <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" version="5" xmlns="http://java.sun.com/xml/ns/javaee"> <display-name>Application</display-name> <module> <web> <web-uri>Application.war</web-uri> <context-root>original_context_root</context-root> </web> </module> </application>
- Create a variable definition
<variable> <name>new_context_root</name> <value>newcontextroot</value> </variable>
- Assign this variable in the application.xml section
<module-override> <module-name>Application.ear</module-name> <module-type>ear</module-type> <module-descriptor external="false"> <root-element>application</root-element> <uri>META-INF/application.xml</uri> <variable-assignment> <name>new_context_root</name> <xpath>/application/module/web/[context-root="original_context_root"]</xpath> <operation>replace</operation> </variable-assignment> </module-descriptor> </module-override>
WAR with context root defined in weblogic.xml
weblogic.xml
<?xml version = '1.0' encoding = 'windows-1252'?> <weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"> <context-root>original_context_root</context-root> </weblogic-web-app>
- In your generated plan.xml, locate this variable
<variable> <name>WeblogicWebApp_ContextRoots_xxxxxxxxxxxxxx</name> <value xsi:nil="true"></value> </variable>
Remove
xsi:nil="true"
attribute in the<value>
element and define the value<variable> <name>WeblogicWebApp_ContextRoots_xxxxxxxxxxxxxx</name> <value>newcontextroot</value> </variable>
- Find this variable assignment and add
<operation>
element<variable-assignment> <name>WeblogicWebApp_ContextRoots_xxxxxxxxxxxxxx</name> <xpath>/weblogic-web-app/context-root</xpath> <operation>replace</operation> </variable-assignment>
Changing the data source
To change the datasource, you have to create the data source in your code with java:comp/env
prefix. Then define resource in web.xml and finally through weblogic.xml change the JNDI. Resource should be defined by the developer directly in the web.xml, since the deployer may not know what is used in the code. But it is also possible to add this with the deployment plan.
Without resource defined in web.xml
- Create four variables
<variable> <name>resrefname</name> <value>codeDataSource</value> </variable> <variable> <name>restype</name> <value>javax.sql.DataSource</value> </variable> <variable> <name>resauth</name> <value>Container</value> </variable> <variable> <name>jndiname</name> <value>weblogicjndi</value> </variable>
- Assign resrefname, restype and resauth variables to web.xml section in the deployment plan with add operation
<variable-assignment> <name>resrefname</name> <xpath>/web-app/resource-description/res-ref-name</xpath> <operation>add</operation> </variable-assignment> <variable-assignment> <name>restype</name> <xpath>/web-app/resource-description/[res-ref-name="codeDataSource"]/res-type</xpath> <operation>add</operation> </variable-assignment> <variable-assignment> <name>resauth</name> <xpath>/web-app/resource-description/[res-ref-name="codeDataSource"]/res-auth</xpath> <operation>add</operation> </variable-assignment>
- Assign resrefname and jndiname variable to weblogic.xml section in the deployment plan with add operation
<variable-assignment> <name>resrefname</name> <xpath>/weblogic-web-app/resource-description/res-ref-name</xpath> <operation>add</operation> </variable-assignment> <variable-assignment> <name>jndiname</name> <xpath>/weblogic-web-app/resource-description/[res-ref-name="codeDataSource"]/jndi-name</xpath> <operation>add</operation> </variable-assignment>
With resource defined in web.xml
web.xml
<resource-ref> ... <res-ref-name>codeDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> ... </resource-ref>
Just create only resrefname and jndiname variables from the previous example and assign them in weblogic.xml section only.
Why the heck does not the deployment plan work…
- XML file edited by deployment plan has a doctype declaration instead of a namespace attribute and although Weblogic 8.1+ can handle doctype XMLs, the deployment plan is not compatible with it
- Wrong xpath. Seriously, check it.
- Data source in the code is not created with
java:comp/env
prefix.javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("java:comp/env/codeDataSource");
<res-auth>
value is, at least in my case, case sensitive.