Maven:不同配置文件的不同属性文件

Seb*_*ebi 5 java maven-2

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

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

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

这是一个好主意吗?

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

如果不是:解决这个问题会有什么好办法?

pol*_*iel 7

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

        <build>
                    <resources>
        <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering>
        </resource>
    </resources>
             </build>
Run Code Online (Sandbox Code Playgroud)

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

sendEmail=${props.email}
Run Code Online (Sandbox Code Playgroud)

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

对比是:

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