【问题标题】:Maven: Different property files for different profilesMaven:不同配置文件的不同属性文件
【发布时间】:2011-11-30 11:41:31
【问题描述】:

我使用不同的 Maven 配置文件将我的应用程序部署到不同的环境。 (用weblogic-maven-plugin,不过我觉得这不重要)

在应用程序中,我使用 Spring Web Services。现在我想根据环境更改端点。 (端点在 Spring 的 applicationContext.xml 中定义)

我的想法是从属性文件中读取值。此属性文件将在 Maven 打包阶段写入(或复制)。

这是个好主意吗?

如果是:如何使用 maven 创建此属性(或整个文件)。

如果否:解决此问题的更好方法是什么?

【问题讨论】:

    标签: java maven-2


    【解决方案1】:

    我实现了类似的东西,但将变量放在 pom.xml 中而不是在属性文件中。所以我的属性文件中包含 Maven 在打包时会更改的变量。

    首先我在 pom 的配置文件部分定义了这些变量:

    <profiles>
        <profile>
            <id>dev</id>
            <activation><activeByDefault>true</activeByDefault></activation>
            <properties>
                <props.debug>true</props.debug>
                <props.email>false</props.email>
                                <what.ever.you.want>value for dev profile</what.ever.you.want>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <props.debug>false</props.debug>
                <props.email>true</props.email>
                <what.ever.you.want>value for prod profile</what.ever.you.want>
            </properties>
        </profile>
    </profiles>
    

    然后激活maven处理和过滤资源。所以在你的构建部分:

            <build>
                        <resources>
            <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>
            </resource>
        </resources>
                 </build>
    

    最后我可以在我的属性文件和配置文件中包含“vars”。 例如,在我的项目中,我有一个 email.properties 文件用于配置电子邮件发送。属性“sendEmail”指示我是否必须发送电子邮件(产品配置文件)或在调试中打印它(开发配置文件)。使用 dev 配置文件时,此 var 将设置为 false,而使用配置文件 prod 时,该属性将具有 true 值。

    sendEmail=${props.email}
    

    这不仅适用于属性文件,也适用于 XML(我猜是每个文本文件)

    对比如下:

    • 部分配置分散在 pom 文件中
    • Maven 包装更持久(因为过滤)
    • 将 var 放入 XML 文件中会使它们在语法上出错(因为字符 $)

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2012-03-11
      • 2015-06-16
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2012-03-07
      • 2022-01-24
      相关资源
      最近更新 更多