Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference environment variables in Wildfly's standalone.xml config file?

I'm using Wildfly 11 on Mac OS X. In my ~/.profile file, I defined a couple of environment variables, which I can see on my console ...

localhost:bin davea$ echo $DB_USERNAME
user1
localhost:bin davea$ echo $DB_PASSWORD
pwd1

In my $WILDFLY_HOME/standalone/configuration/standalone.xml file, I attempt to reference these variables ...

            <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://localhost:3306/my_db?serverTimezone=CST&amp;autoReconnect=true&amp;useSSL=false</connection-url>
                <driver>mysql</driver>
                <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>10</min-pool-size>
                    <max-pool-size>100</max-pool-size>
                    <prefill>true</prefill>
                </pool>
                <security>
                    <user-name>${env.DB_USERNAME}</user-name>
                    <password>${env.DB_PASSWORD}</password>
                </security>

but when I start my Wildfly instance, I get this error ...

14:35:16,817 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 55) WFLYUT0014: Creating file handler for path '/opt/wildfly-10.0.0.CR2/welcome-content' with options [directory-listing: 'false', follow-symlink: 'false', case-sensitive: 'true', safe-symlink-paths: '[]']
14:35:16,846 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 33) WFLYCTL0013: Operation ("add") failed - address: ([
    ("subsystem" => "datasources"),
    ("data-source" => "MySqlDS")
]) - failure description: "WFLYCTL0211: Cannot resolve expression '${env.DB_USERNAME}'"
14:35:16,855 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-5) WFLYUT0012: Started server default-server.

What's the right way to reference environment variables in Wildfly?

like image 880
Dave Avatar asked Dec 08 '25 17:12

Dave


1 Answers

There are two ways to refer to environment variables in your Wildfly configuration and I think the second option will resolve your issue.

  1. You can export the variable and pass it at command line while starting wildfly server.

    $ export LOGPATH=/home/jboss/log 
    $ ./standalone.sh -DlogPath=LOGPATH
     <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
           <param name="File" value="${logPath}/server.log"/>
               . . . .  
     </appender>
    
  2. Another way is to directly access them using env. prefix.

    <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
         <param name="File" value="${env.LOGPATH}/server.log"/>
             . . . .  
     </appender>
    

    Please note you will need to edit the following file "$WILDFLY_HOME/bin/jboss-cli.xml" and will need to set the following property to "true" (by default that is false)

     <!-- whether to resolve system properties specified as command argument or operation parameter values  
          in the CLI VM before sending the operation requests to the controller -->  
     <resolve-parameter-values>true</resolve-parameter-values> 
    

Try adding these properties to your standalone.xml file.

<subsystem xmlns="urn:jboss:domain:ee:1.1">
     <spec-descriptor-property-replacement>true</spec-descriptor-property-replacement>
     <jboss-descriptor-property-replacement>true</jboss-descriptor-property-replacement>
</subsystem>
like image 180
Atul Avatar answered Dec 10 '25 21:12

Atul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!