Maven读取属性文件中的环境变量
Maven read environment variable in properties file
如何读取属性文件中的系统环境变量。我正在使用 MyBatis maven 插件进行数据库迁移。 MyBatis 使用基于环境的属性文件。我正在尝试读取属性文件中的环境变量,例如:
development.properties
username=${env.username}
password=${env.password}
Error: FATAL: role "${env.username}" does not exist
我将用户名和密码存储在 mac 上的“.profile”文件中。读取这些变量的正确方法是什么?
这似乎在 mybatis/migrations
issue 114 中讨论过:
Migrations or migrations-maven-plugin itself does not provide filtering (i.e. variable substitution).
But Maven's resource plugin does. See Maven Filtering.
You can use the filtering along with env/system/even settings.xml
values using pom.xml
properties.
您可能应该首先使用 maven 资源插件过滤属性文件。之后 myBatis 插件应该可以正常工作了。
有关资源插件的简短示例,请参阅以下 gist。
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.Whosebug</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
如果您想要获取并加载 class 中的 属性 值。您可以使用以下代码:
String userName = DatabasePropertyHandler.resolveEnvVars(${env.username});
public static String resolveEnvVars(String input)
{
if (null == input)
{
return null;
}
// match ${ENV_VAR_NAME} or $ENV_VAR_NAME
Pattern p = Pattern.compile("\$\{(\w+)\}|\$(\w+)");
Matcher m = p.matcher(input); // get a matcher object
StringBuffer sb = new StringBuffer();
while(m.find()){
String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
String envVarValue = System.getenv(envVarName);
m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);
}
m.appendTail(sb);
return sb.toString();
}
如何读取属性文件中的系统环境变量。我正在使用 MyBatis maven 插件进行数据库迁移。 MyBatis 使用基于环境的属性文件。我正在尝试读取属性文件中的环境变量,例如:
development.properties
username=${env.username}
password=${env.password}
Error: FATAL: role "${env.username}" does not exist
我将用户名和密码存储在 mac 上的“.profile”文件中。读取这些变量的正确方法是什么?
这似乎在 mybatis/migrations
issue 114 中讨论过:
Migrations or migrations-maven-plugin itself does not provide filtering (i.e. variable substitution).
But Maven's resource plugin does. See Maven Filtering.You can use the filtering along with env/system/even
settings.xml
values usingpom.xml
properties.
您可能应该首先使用 maven 资源插件过滤属性文件。之后 myBatis 插件应该可以正常工作了。
有关资源插件的简短示例,请参阅以下 gist。
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.Whosebug</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
如果您想要获取并加载 class 中的 属性 值。您可以使用以下代码:
String userName = DatabasePropertyHandler.resolveEnvVars(${env.username});
public static String resolveEnvVars(String input)
{
if (null == input)
{
return null;
}
// match ${ENV_VAR_NAME} or $ENV_VAR_NAME
Pattern p = Pattern.compile("\$\{(\w+)\}|\$(\w+)");
Matcher m = p.matcher(input); // get a matcher object
StringBuffer sb = new StringBuffer();
while(m.find()){
String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
String envVarValue = System.getenv(envVarName);
m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);
}
m.appendTail(sb);
return sb.toString();
}