Maven read environment variable in properties file

混江龙づ霸主 提交于 2021-02-08 07:44:30

问题


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

I stored username and password in a ".profile" file on a mac. What's the correct way to read those variables?


回答1:


You should probably first filter the properties file with the maven resources plugin. Afterwards myBatis plugin should work as intended.

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>

Command line:

username=user1 password=pass1 mvn resources:resources && cat target/classes/development.properties

Console log:

(...)
[INFO] --- maven-resources-plugin:2.3:resources (default-cli) @ question-48693420 ---
(...)
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(...)

username=user1
password=pass1



回答2:


That seems to be discusses in 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.




回答3:


If you want get and load the property value in class. You can use below code :

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();
      }


来源:https://stackoverflow.com/questions/48693420/maven-read-environment-variable-in-properties-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!