Search code examples
javalinuxjbossenvironment-variableswildfly

Use Environment Variables in Standalone.xml for WildFly application on Linux VM


I am having trouble getting environment variables to resolve On a Linux VM when I start up a WildFly java application.

<system-properties>
        <property name="java.util.logging.manager" value="org.jboss.logmanager.LogManager"/>
        <property name="appname.url" value="https://${env.Environment}/appname"/>
</system-properties>

When I echo that environment variable (defined in /etc/environment) I get the following:

[ ~ ]$ echo $Environment
tst

I've tried updating the standalone.xml to have this these two properties set to true:

<subsystem xmlns="urn:jboss:domain:ee:4.0">
            <spec-descriptor-property-replacement>true</spec-descriptor-property-replacement>
            <jboss-descriptor-property-replacement>true</jboss-descriptor-property-replacement>

Also tried updating bin/jboss-cli.xml with this setting set to true:

<resolve-parameter-values>true</resolve-parameter-values>

Starting the application using it's service (i.e., systemctl restart appname) throws errors in the logs like this:

ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([("system-property" => "appname.url")]) - failure description: "WFLYCTL0211: Cannot resolve expression 'https://${env.Environment}/appname'"

Unfortunately, this does not resolve my issue. Any ideas?


Solution

  • Solution for this involved a few steps to get environment variables recognized by the WildFly app's service. It isn't exactly an environment variable, it's the closest you can get. Per this thread on stackexchange, service strips all environment variables, so they cannot be referenced. Therefore, you must create a file that stores those variables and then source it in startup.

    Here they are:

    1. Update bin/standalone.conf with a new line to include JAVA_OPTS="$JAVA_OPTS -Denv.Environment=$Environment"
    2. Create /etc/default/appname file so it can be sourced later on. That file only contains the line Environment=tst
    3. Update /etc/init.d/appname file with
      [ -f /etc/default/appname ] && . /etc/default/appname
      export Environment
      
    4. reload systemctl's daemon systemctl daemon-reload
    5. restart the service systemctl restart appname