Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven read environment variable in properties file

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?

like image 925
lch Avatar asked Dec 07 '22 15:12

lch


1 Answers

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
like image 85
Christian Avatar answered Dec 23 '22 17:12

Christian