Search code examples
configurationopen-libertymicroprofile

OpenLiberty can not inject environment variable


I am trying to inject a property which is defined in server.env.

 @Inject
 @ConfigProperty(name = "property")
 private String serverProperty;

content of server.env

property=server-env-property

I have added it inside pom.xml

<serverEnv>src/main/liberty/config/server.env</serverEnv>

It is read during liberty-maven-plugin:2.2:package-server

 CWWKM2144I: Update server configuration file server.env from /Users/abalaniuc/code/config/src/main/liberty/config/server.env.

However when application is executed I got this error:

 The property property was not found in the configuration.

Stack trace:

[ERROR   ] CWMCG5003E: The [BackedAnnotatedField] @Inject @ConfigProperty private com.microprofile.study.config.config.ConfigTestController.serverProperty InjectionPoint dependency was not resolved. Error: java.util.NoSuchElementException: CWMCG0015E: The property property was not found in the configuration.
        at com.ibm.ws.microprofile.config.impl.AbstractConfig.getValue(AbstractConfig.java:175)
        at [internal classes]

Obviously I am missing something, but can't figure out what exactly. Can you please help me?

As was suggested in the answers below, I have removed . from a property name, but still getting same result.

Liberty-plugin configuration:

<plugin>
  <groupId>net.wasdev.wlp.maven.plugins</groupId>
  <artifactId>liberty-maven-plugin</artifactId>
  <version>${openliberty.maven.version}</version>
  <executions>
    <execution>
      <id>package-server</id>
      <phase>package</phase>
      <goals>
        <goal>create-server</goal>
        <goal>install-apps</goal>
        <goal>package-server</goal>
      </goals>
      <configuration>
        <outputDirectory>target/wlp-package</outputDirectory>
      </configuration>
    </execution>
  </executions>
  <configuration>
    <assemblyArtifact>
      <groupId>io.openliberty</groupId>
      <artifactId>openliberty-runtime</artifactId>
      <version>${openliberty.version}</version>
      <type>zip</type>
    </assemblyArtifact>
    <configFile>src/main/liberty/config/server.xml</configFile>
    <serverEnv>src/main/liberty/config/server.env</serverEnv>
    <bootstrapPropertiesFile>src/main/liberty/config/bootstrap.properties</bootstrapPropertiesFile>
    <appArchive>${project.build.directory}/${final.name}.war</appArchive>
    <packageFile>${project.build.directory}/${final.name}.jar</packageFile>
    <include>runnable</include>
    <serverName>${final.name}</serverName>
    <installAppPackages>project</installAppPackages>
  </configuration>
</plugin>

To start application I do:

mvn clean package

java -jar target/config.jar

Solution

  • Environment variables don't usually allow periods - see this post for more details. But you can use underscores in environment variables.

    MicroProfile Config is supposed to convert underscores from env vars into periods - it is also supposed to re-map case sensitivity. The mapping is discussed here.

    So, my suggestion would be to try changing server.property=server-env-property in server.env to be server_property=server-env-property or SERVER_PROPERTY=server-env-property to see if that works.

    UPDATE: The main problem is the way that the env vars are defined for the environment that the Liberty server is running in.

    When using the server command to start/run the server, it will read the server's server.env file and set these environment variables for the server (in addition to any environment variables already defined on the shell).

    When using the java -jar server.jar approach, it will not read the server.env file, but will read the environment variables defined on the shell. When using this approach, users should either explicitly set the environment variables (i.e. export MY_VAR=MyValue) or should use shell-specific commands to read in the server.env file (i.e. . server.env, env X=Y, etc.).

    Hope this helps!