Where to put my application specific properties files outside WAR/EAR in JBoss EAP

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x
    • 7.x
    • 8.x

Issue

  • Is there a place or directory which can be used to put the properties files or additional files which are supposed to be loaded via classloader.
  • How can we make these properties files accessible to various applications deployed on JBoss EAP6.
  • Unable to read the properties file from module my.conf which is placed inside the $JBOSS_HOME/modules directory and facing the below exception:
unable to find sample.properties
  • How to add custom properties files for each application in EAP 6?
  • In EAP 5.0 , there was a conf folder, where we could add custom properties files for each application we deploy. How can we achieve the same in JBoss EAP 6 domain mode?

Resolution

  • Create a custom module where you put your properties files and put jboss-deployment-structure.xml to your application archive (WAR/EAR) to use that custom module. If you would like to create a custom module for putting properties files and make it globally accessible from all deployments, please refer to https://access.redhat.com/knowledge/solutions/122883.
  1. Create the new module directory under $JBOSS_HOME/modules (using my/conf in this example)

     mkdir -p $JBOSS_HOME/modules/my/conf/main/properties/
    
  2. Put your properties files under $JBOSS_HOME/modules/my/conf/main/properties/ directory

  3. Create a module.xml here $JBOSS_HOME/modules/my/conf/main/module.xml

     <module xmlns="urn:jboss:module:1.1" name="my.conf">
         <resources>
             <resource-root path="properties"></resource-root>
         </resources>
     </module>
    

    Make sure properties and module.xml are present in same directory.

  4. Put the following jboss-deployment-structure.xml to your application archive (WAR/EAR) to use the above custom module:

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

    Or, add the global module in configuration file ($JBOSS_HOME/standalone/configuration/standalone(-*).xml):

     <subsystem xmlns="urn:jboss:domain:ee:1.0" >            
         <global-modules>
             <module name="my.conf" slot="main" />            
         </global-modules>
     </subsystem>
    
  5. Then you can access your properties files using the code below (example assumes you have a my.properties file in $JBOSS_HOME/modules/my/conf/main/properties/)

     try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties")) {
         Properties props = new Properties();
         props.load(in);
         props.getProperty("mykey");
     } catch (IOException e) {
         // Error handling
     }
    
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.