2

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
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53

2 Answers2

3

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!

Andy McCright
  • 1,273
  • 6
  • 8
  • Unfortunately it doesn't solve the issue. Still got same error even if I use `server_property=server-env-property`, or just `server=server-env-property`. I guess the file is not expected during the property lookup – Anton Balaniuc Sep 23 '19 at 15:39
  • did you also update your `@ConfigProperty` annotation with the updated property name? Also, as a sanity check you could try printing the result of `System.getenv("server_property")` to see if this issue is the server.env setting the variable, or an issue with MP Config finding/reading the property – Andy Guibert Sep 23 '19 at 15:51
  • @AndyGuibert so `System.getenv("property")` returns `null`. I have changed the name from `server.property` to `property` in both `server.env` and `@ConfigProperty(name = "property")` – Anton Balaniuc Sep 23 '19 at 16:09
  • interesting, where is your server.env file located, and what tool is being used to start the Liberty server? – Andy Guibert Sep 23 '19 at 16:14
  • Look at `target/liberty/wlp/user/servers//server.env`, (by default: `target/liberty/wlp/user/servers/defaultServer/server.env`). What's in there? – Scott Kurz Sep 23 '19 at 16:26
  • You might also just share your complete configuration of the `liberty-maven-plugin`.. including whether you're inheriting from one of the parent POMs. If your problem is ultimately related to plugin config then you might even consider moving up to the newly-released v3.0.1 level, where a number of enhancements and improvements have been made, rather than sorting things out at the current level. – Scott Kurz Sep 23 '19 at 17:08
  • @ScottKurz, you can review the whole pom.xml here https://github.com/anton-balaniuc/config/blob/master/pom.xml, I there is no parent pom. `liberty-maven-plugin` is used directly. – Anton Balaniuc Sep 24 '19 at 09:24
  • This worked fine for me.. I did: `mvn clean install liberty:start-server` then hit endpoint **http://localhost:8181/config/data/config/server** and it shows: `Config value as Injected by CDI server-env-property`. – Scott Kurz Sep 24 '19 at 11:06
  • @ScottKurz, it is working indeed if you use `mvn clean install liberty:start-server`, however I was using different approach which is "suggested" by `https://start.microprofile.io/`, when you generate project the readme.txt has the following instructions `mvn clean package` and `java -jar target/config.jar` – Anton Balaniuc Sep 25 '19 at 13:26
  • @ScottKurz, so if I use `mvn clean package and java -jar target/config.jar` it looks like server.env is not used/found. – Anton Balaniuc Sep 25 '19 at 13:27
  • @AntonBalaniuc - ah! That is expected. The java -jar approach does not read the server.env file. Instead it reads your shells environment variables. I ran your test case and saw the error. Then I exported property=someProp on the terminal, and it succeeded. Can you give that a try: `export property=someProp` and then run `java -jar config.jar` ? – Andy McCright Sep 25 '19 at 15:22
  • @AndyMcCright, Thanks a lot it works. ` The java -jar approach does not read the server.env file. Instead it reads your shells environment variables.` is this part documented somewhere? – Anton Balaniuc Sep 30 '19 at 12:01
  • @AndyMcCright, can you please as well update your answer with this information so I can accept it? – Anton Balaniuc Sep 30 '19 at 12:21
2

A period isn't a valid character in an environment variable. If you specify this in server.env it should work:

server_property=server-env-property

the MicroProfile config spec will automatically convert the period in the ConfigProperty annotation into an _ for the lookup.

Alasdair
  • 3,071
  • 15
  • 20
  • Unfortunately it doesn't solve the issue. Still got same error even if I use `server_property=server-env-property`, or just `server=server-env-property`. I guess the file is not expected during the property lookup – Anton Balaniuc Sep 23 '19 at 15:39