问题描述
如何读取属性文件中的系统环境变量.我正在使用 MyBatis maven 插件进行数据库迁移.MyBatis 使用基于环境的属性文件.我正在尝试读取属性文件中的环境变量,例如:
How to read system environment variables in a properties file. I am using MyBatis maven plugin for database migrations. MyBatis use properties file basing on the environment. I am trying to read environment variable inside properties file like:
development.properties
username=${env.username}
password=${env.password}
Error: FATAL: role "${env.username}" does not exist
我将用户名和密码存储在 mac 上的.profile"文件中.读取这些变量的正确方法是什么?
I stored username and password in a ".profile" file on a mac. What's the correct way to read those variables?
推荐答案
您可能应该首先使用 maven 资源插件过滤属性文件.之后 myBatis 插件应该可以正常工作了.
You should probably first filter the properties file with the maven resources plugin. Afterwards myBatis plugin should work as intended.
请参阅以下 gist 以获取资源插件的简短示例.
See following gist for a short example to the resources plugin.
development.properties
# place in src/main/resources
username=${env.username}
password=${env.password}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>question-48693420</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
命令行:
username=user1 password=pass1 mvn resources:resources && cat target/classes/development.properties
控制台日志:
(...)
[INFO] --- maven-resources-plugin:2.3:resources (default-cli) @ question-48693420 ---
(...)
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(...)
username=user1
password=pass1
这篇关于Maven读取属性文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!