Thursday, October 24, 2013

Changing Classloader policy in RAD for Websphere Applications

Changing Classloader policy in RAD for Websphere Applications

Sometimes the need arises to make sure the jars in your application get used over class definitions that may be part of the server runtime.  This is true when using Axis2 as your web service engine in Websphere.
To do this follow these instructions below:
In IBM’s RAD, go to your EAR project and open the application.xml file.   Be sure to do this in a JEE like perspective so that you get to use RAD’s special Application Deployment Descriptor UI.
 

At the bottom of the “Application Deployment Descriptor” UI, go to the Deployment tab. And change the “Classloader Mode” to Parent_Last.  I usually do this for both the EAR and the WAR.



This is all you have to do for local testing and development.  Now if you publish to the local WAS test environment, your application should first look for class definitions from the jars in your WEB-INF/lib before looking at the server runtime…(you  may need to restart the server)

Maven build changes.
When you follow the directions above to change the classloader policy, it will create some extra files under the META-INF folder under the ear project.... to get Maven to package these files with your app you have to add a resources section to your ear pom.
<modelVersion>4.0.0</modelVersion>
<artifactId>mobius-ear</artifactId>
<name>Mobius EAR</name>
<packaging>ear</packaging>
<build>
<resources>
<resource>
<directory>META-INF</directory>
<targetPath> ../${project.artifactId}-${project.version}/META-INF</targetPath>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>mobius-ws</artifactId>
<bundleFileName>mobius-ws.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>  
Isn’t there another way…
You can also change the classloader policy via the Websphere Admin console, but depending your Enterprise environments, you as a developer may not have access to Admin Console… so following the steps above, will automatically change the class loader policy when you application is deployed without requiring changing the classloader policy through the admin console.