从Heroku配置的环境变量触发Maven配置文件

7
我正在尝试激活我的Maven POM文件中的配置文件,方法如下:
    <profile>
        <id>test</id>
        </properties>
        <activation>
            <property>
                <name>env.SITE</name>
                <value>test</value>
            </property>
        </activation>
    </profile>

现在我正在配置Heroku,使环境变量SITE如下:

heroku config:add SITE=test.

我期望当代码被推送时,环境变量将触发配置文件。然而这个并没有发生。

5个回答

13

您可以不需要自定义构建包即可完成此操作。

在您的pom.xml中使用此代码片段显示Heroku上可用的所有属性并选择一个不在您本地的属性:http://florianlr.wordpress.com/2012/04/24/16/

我使用了env.DYNO。

    <profile>
        <id>heroku</id>
        <activation>
            <property>
                <name>env.DYNO</name>
            </property>
        </activation>
    ...
    </profile>
    ...

运作得非常好:)


太棒了。这应该被接受为更好的答案。+1 - fegemo

3
您可以使用环境变量 MAVEN_CUSTOM_OPTS 指定maven配置文件。
在pom文件中,通过定义基于属性的激活maven配置文件。以下示例将使用开发配置文件(如果您指定)并默认使用生产配置文件。 参考文档
<profiles>
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>!profile-development</name>
            </property>
        </activation>
    </profile>
    <profile>
        <id>development</id>
        <activation>
            <property>
                <name>profile-development</name>
            </property>
        </activation>
    </profile>
</profiles>

在Heroku中设置MAVEN_CUSTOM_OPTS环境变量以使用该配置文件。请注意,此变量将覆盖默认选项-DskipTests参考文档
MAVEN_CUSTOM_OPTS=-DskipTests -Dprofile-development

在这个示例中,生产配置文件不需要声明MAVEN_CUSTOM_OPTS

2
您可以介绍自己定制的Maven settings.xml文件,例如heroku-settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <!-- activate by setting the MAVEN_SETTINGS_PATH config var to heroku-settings.xml in Heroku project settings tab.
    See https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml for more details.
     -->

    <activeProfiles>
        <activeProfile>production</activeProfile>
    </activeProfiles>
</settings>

然后在Heroku项目设置选项卡中将MAVEN_SETTINGS_PATH配置变量设置为heroku-settings.xml以激活设置。

最初的回答

1

目前在编译时无法使用配置变量。要更改Maven配置文件,您需要分叉Heroku Java Buildpack


1
我通过添加如下的Heroku环境来解决了我的问题:
MAVEN_CUSTOM_OPTS=-P<profile_id>

并从pom.xml中删除“activation”标签。

顺便说一句,我不知道为什么这不起作用(在Heroku中我有ENVIRONMENT=test)

<profile>
  <id>test</id>
  <activation>
    <property>
      <name>env.ENVIRONMENT</name>
      <value>test</value>
    </property>
  </activation>
...
</profile>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接