【发布时间】:2019-09-23 15:12:37
【问题描述】:
我正在尝试注入在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
【问题讨论】:
-
您可以将您的示例应用程序发布到 GitHub 吗?我想知道在服务器打包期间是否正在读取 env 变量,而不是在服务器运行时。
-
@AndyMcCright,请在此处找到 repo github.com/anton-balaniuc/config
标签: configuration open-liberty microprofile