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&autoReconnect=true&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?
There are two ways to refer to environment variables in your Wildfly configuration and I think the second option will resolve your issue.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With