Monday, 19 November 2012

Configuring static resources for use within the application in JBoss AS 7

In previous versions of JBoss, you could link an external directory into the classpath and load static resources like images/CSS/JS into the application using the WEB-INF/context.xml

<Context allowLinking="true" cookies="true" crossContext="true" override="true"> 
<Resources allowLinking="true" homeDir="/mydomain/staticfiles" /> 

In Weblogic servers, there is the virtual-directory-mapping to achieve this.

In Jboss 7 this no longer works, and you need to bundle the static files within the EAR or War. Note: If you need to serve these over the browser like a public web server as static files then you need a servlet method like this

But if you want to load a static property file or other content into your webapp, the solution provided by Jboss is what this post is about. You can define a “module” within JBOSS_HOME and store the files there or extend that to any location on the filesystem.

The base reference for this solution is on the Jboss forums How to put an external file in the classpath

Please read that for the theory behind this. Actual steps to be used to store and load any static files or properties or config etc:

1. Create a new module for your configuration or static files - at this location


<?xml version="1.0" encoding="UTF-8"?> 
<module xmlns="urn:jboss:module:1.1" name="com.mycompany"> <resources> 
<resource-root path="."/> 

2. Add your properties files or images to the module at this location








3. Add your module to your application classpath in a jboss-deployment-structure.xml file

<?xml version="1.0" encoding="UTF-8"?> 
<jboss-deployment-structure> <deployment> <dependencies> 
<module name="com.mycompany" />

This file must be placed either in the META-INF directory of your EAR file or the WEB-INF directory of your WAR file. See Class Loading in AS7 for more information.

OR add your module to your application classpath using a MANIFEST.MF entry in the META-INF of the EAR

Manifest-Version: 1.0

Dependencies: com.mycompany

5. Load a properties file from the classloader in your application

URL imgUrl = this.getClass().getClassLoader().getResource("logo.jpg");

As you’ve noticed this sits within the JBOSS_HOME/modules directory. You can further change the location of this by editing the standalone.bat or files as follows



"-Dorg.jboss.boot.log.file=%JBOSS_LOG_DIR%\boot.log" ^

"-Dlogging.configuration=file:%JBOSS_CONFIG_DIR%/" ^

-jar "%JBOSS_HOME%\jboss-modules.jar" ^

-mp "%JBOSS_MODULEPATH%;d:\mycustommodules" ^

-jaxpmodule "javax.xml.jaxp-provider" ^ ^

-Djboss.home.dir="%JBOSS_HOME%" ^


And the corresponding structure on Windows platform looks like this







In this way your static directory does not need to be within the JBOSS folder at all.