Configuration OpenLiberty无法注入环境变量

Configuration OpenLiberty无法注入环境变量,configuration,open-liberty,microprofile,Configuration,Open Liberty,Microprofile,我正在尝试注入一个在server.env中定义的属性 @Inject @ConfigProperty(name = "property") private String serverProperty; server.env的内容 property=server-env-property 我在pom.xml <serverEnv>src/main/liberty/config/server.env</serverEnv> 但是,在执行应用程序时,我遇到了以下错误

我正在尝试注入一个在
server.env
中定义的属性

 @Inject
 @ConfigProperty(name = "property")
 private String serverProperty;
server.env的内容

property=server-env-property
我在
pom.xml

<serverEnv>src/main/liberty/config/server.env</serverEnv>
但是,在执行应用程序时,我遇到了以下错误:

 The property property was not found in the configuration.
堆栈跟踪:

[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]
很明显,我遗漏了一些东西,但不知道到底是什么。你能帮帮我吗

正如下面的答案所建议的,我已经从属性名称中删除了
,但仍然得到了相同的结果

Liberty插件配置:

<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>

句点不是环境变量中的有效字符。如果您在server.env中指定此选项,则它应该可以工作:

server_property=server-env-property

MicroProfile config spec将自动将
ConfigProperty
注释中的句点转换为

环境变量通常不允许句点-有关详细信息,请参阅。但您可以在环境变量中使用下划线

MicroProfile配置应该将环境变量中的下划线转换为句点-它还应该重新映射大小写敏感度。对映射进行了讨论

因此,我的建议是尝试将server.env中的
server.property=server env property
更改为
server\u property=server env property
server\u property=server env property
,看看是否有效

更新:主要问题是为Liberty服务器运行的环境定义环境变量的方式

使用
server
命令启动/运行服务器时,它将读取服务器的server.env文件,并为服务器设置这些环境变量(除shell上已定义的任何环境变量外)

当使用
java-jarserver.jar
方法时,它不会读取server.env文件,而是读取shell上定义的环境变量。使用此方法时,用户应明确设置环境变量(即
export MY_VAR=MyValue
),或使用特定于shell的命令读取server.env文件(即
.server.env
env X=Y
,等等)


希望这有帮助

不幸的是,这并不能解决问题。即使我使用了
server\u property=server env property
,或者只是
server=server env property
,仍然会出现相同的错误。我猜在属性查找期间不需要该文件。您是否也使用更新的属性名称更新了
@ConfigProperty
注释?此外,作为一项健全性检查,您可以尝试打印
System.getenv(“server\u属性”)
的结果,以查看此问题是server.env设置变量的问题,还是MP Config查找/读取变量的问题property@AndyGuibert因此
System.getenv(“属性”)
返回
null
。我已经将
server.env
@ConfigProperty(name=“property”)
中的名称从
server.property
更改为
property
,有趣的是,您的server.env文件位于哪里,以及启动Liberty服务器使用了什么工具?请查看
target/Liberty/wlp/user/servers//server.env
,(默认情况下:
target/liberty/wlp/user/servers/defaultServer/server.env
)。里面有什么?不幸的是,它没有解决问题。即使我使用
server\u property=server env property
,或者只是
server=server env property
,仍然会出现相同的错误。我猜在属性查找期间不需要该文件。您可以将示例应用程序发布到GitHub吗?我想知道在查找期间是否正在读取env变量服务器打包,但不是在服务器运行时。@AndyMcCright,请在此处找到repo
mvn clean package

java -jar target/config.jar
server_property=server-env-property