Maven:不同配置文件的不同属性文件 [英] Maven: Different property files for different profiles

查看:221
本文介绍了Maven:不同配置文件的不同属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用不同的maven配置文件将我的应用程序部署到不同的环境。 (使用weblogic-maven-plugin,但我认为这并不重要)

I'm using different maven profiles to deploy my application to different environments. (With the weblogic-maven-plugin, but I think this is not important)

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

In the application I use Spring Web Services. Now I'd like to change the endpoint depending on the environment. (The endpoint is defined in Spring's applicationContext.xml)

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

My idea is to read the value from a property file. This property file will be written (or copied) during Mavens package phase.

这是个好主意吗?

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

If yes: How do I create this property (or the whole file) with maven.

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

If no: What would be a better aproach to solve this problem?

推荐答案

我实现了类似的东西,但是在<$中有变量c $ c> pom.xml 而不是propreties文件。所以我的属性文件包含Maven在包装中会改变的变量。

I achieved something similar, but having the variables in the pom.xml instead in propreties files. So my properties files have variables that would be changed by Maven in packaging.

首先我在pom的profiles部分定义了这些变量:

First I defined these vars in the profiles section of the 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处理和资源过滤。所以在你的构建部分:

Then activated the maven procesing and filtering of resources. So in your build section:

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

最后,我的属性文件,配置文件中可以包含vars。
例如,在我的项目中,我有一个 email.properties 文件,用于配置发送的电子邮件。属性sendEmail表示我是否必须发送电子邮件(prod配置文件)或在debug(dev配置文件)中打印它。使用dev配置文件时,此var将被置为false,而使用配置文件prod时,属性将具有真值。

Finally I can have "vars" in my properties files, configuration files. For example, In my project I have a email.properties file for configuring the emails sending. The property "sendEmail" indicates if I have to send the email (prod profile) or to print it in debug (dev profile). With dev profile this var will be put to false whereas with the profile prod the property will have the true value.

sendEmail=${props.email}

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

This not only works with properties files, also with XML (I guess with every text file)

对比是:


  • 部分配置是分散的在pom文件中

  • Maven包装持续更多(因为过滤)

  • 将vars放入XML文件会使它们语法错误(因为字符$ )

这篇关于Maven:不同配置文件的不同属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆