【问题标题】:Passing env variables in maven command [duplicate]在maven命令中传递env变量[重复]
【发布时间】:2019-01-17 07:20:30
【问题描述】:

我有一个如下所示的属性文件

testdb.username=AA
testdb.password=AA
testdb.port=1521
testdb.host=localhost

现在,我想在 maven 命令中将所有这 4 个键值对作为 env 变量传递。

一种方法是将每个键值对放置在如下所示的 maven 命令中

mvn clean package -Dtestdb.username=A -Dtestdb.password=AA ....

我想知道 Maven 中是否有任何方法可以将整个属性文件传递给 Maven 和 Maven 读取属性文件并将所有键值对动态设置为 Maven 命令中的 env 变量。

【问题讨论】:

    标签: java maven


    【解决方案1】:

    环境变量在maven中是这样引用的:

    <properties>
        <testdb.username>${env.ENV_USERNAME}</testdb.username>
        <testdb.password>${env.ENV_PASSWORD}</testdb.password>
        <testdb.port>${env.ENV_PORT}</testdb.port>
        <testdb.host>${env.ENV_HOST}</testdb.host>
    </properties>
    

    但是,我认为你想做的是这样的:

    <properties>
        <!-- Default values -->
        <testdb.username>foo</testdb.username>
        <testdb.password>AA</testdb.password>
        <testdb.port>1521</testdb.port>
        <testdb.host>localhost</testdb.host>
    </properties>
    
    ...
    
    ${testdb.username}
    
    mvn clean package -Dtestdb.username=$USERNAME -Dtestdb.password=$PASSWORD -Dtestdb.port=$PORT -dtestdb.host=$HOST
    

    希望这些信息对您有用!

    【讨论】:

    • 我认为你需要在目标之前传递 env 变量,而不是相反
    • @Ghilteras 在目标之前或之后并不重要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2019-04-03
    • 2021-11-20
    • 2022-01-19
    • 2014-10-06
    • 2021-11-30
    相关资源
    最近更新 更多