我正在尝试注入(inject)在 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>
在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.
但是,当执行应用程序时,我收到此错误:
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>
要启动应用程序,我会执行以下操作:
mvn clean package
java -jar target/config.jar
最佳答案
环境变量通常不允许使用句点 - 请参阅 this post更多细节。但您可以在环境变量中使用下划线。
MicroProfile Config 应该将下划线从环境变量转换为句点 - 它还应该重新映射区分大小写。讨论了映射here .
因此,我的建议是尝试将 server.env 中的 server.property=server-env-property
更改为 server_property=server-env-property
或 SERVER_PROPERTY=server-env-property
看看是否有效。
更新:主要问题是为 Liberty 服务器运行的环境定义环境变量的方式。
当使用server
命令启动/运行服务器时,它将读取服务器的server.env文件并为服务器设置这些环境变量(除了服务器上已经定义的任何环境变量)外壳)。
当使用java -jar server.jar
方法时,它不会读取server.env文件,而是读取shell上定义的环境变量。使用此方法时,用户应显式设置环境变量(即 export MY_VAR=MyValue
),或者应使用特定于 shell 的命令读取 server.env 文件(即 .server.env)。 env
、env X=Y
等)。
希望这有帮助!
关于configuration - OpenLiberty 无法注入(inject)环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58065407/