Home > OS >  With Open Liberty, how to use an environment variable as an application property?
With Open Liberty, how to use an environment variable as an application property?

Time:06-23

I expected to be able to do something like this in my server.xml, but I could not get it to work:

<variable name="my.ldap.url" value="${LDAP_URL}" />

Setting it in jvm.options or server.env do not seem to be options either from what I can tell. Neither of those are parsing environment variables from my tests.

-Dmy.ldap.url=$LDAP_URL

Setting properties in my own files and using environment variables did not seem to work either. I tried something like this in my server.xml

<library id="configs">
    <fileset dir="${server.config.dir}/configs" includes="*.properties" />
</library>

So I'm just lost for ideas right now. How do I take environment variables and inject them into an Open Liberty application as application properties?

I searched high and low for documentation on this, with no success.

In Jboss I was able to accomplish simple via

bin/standalone.sh -Dmy.ldap.url=$LDAP_URL

CodePudding user response:

It should work for you exactly the way it does in Jboss:

wlp/bin/server start server1 -Dmy.ldap.url=$LDAP_URL

Your <variable> definition would create a Liberty configuration variable that resolves to the value of LDAP_URL in the environment, but it would not be set as a java system property.

If you're still seeing problems with passing the value on the command line you could check all java system properties by running:

wlp/bin/server dump {server name}

In the output zip file, the dump_{timestamp}/introspections/JavaRuntimeInformation.txt will show both the command line arguments and all defined java system properties.

CodePudding user response:

I'd consider using Microprofile config, checkout this guide and see it does answer your questions.

It shows you how to inject variables using following schema:

@Inject @ConfigProperty(name="port")
private int port;

assuming there is PORT env entry in your system.

You can also use env variables in server configuration using server.xml for example:

<properties.db2.jcc serverName="${JDBC_HOST}" portNumber="${JDBC_PORT}" databaseName="${JDBC_DB}" sslConnection="${JDBC_SSL}"
            user="${JDBC_ID}" password="${JDBC_PASSWORD}"/>

Details here https://openliberty.io/docs/latest/reference/config/server-configuration-overview.html

For your library point you would have to add it to your application via classloader, so I wouldn't recommend that. Like this:

<application name="myapp" type="ear" location="myapp.ear">
  <classloader commonLibraryRef="configs" />
</application>

Hope it solves your problems.

  • Related