简体   繁体   English

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

[英]Maven: Different property files for different profiles

I'm using different maven profiles to deploy my application to different environments. 我正在使用不同的maven配置文件将我的应用程序部署到不同的环境。 (With the weblogic-maven-plugin, but I think this is not important) (使用weblogic-maven-plugin,但我认为这并不重要)

In the application I use Spring Web Services. 在应用程序中,我使用Spring Web Services。 Now I'd like to change the endpoint depending on the environment. 现在我想根据环境更改端点。 (The endpoint is defined in Spring's applicationContext.xml) (端点在Spring的applicationContext.xml中定义)

My idea is to read the value from a property file. 我的想法是从属性文件中读取值。 This property file will be written (or copied) during Mavens package phase. 在Mavens包阶段期间将写入(或复制)此属性文件。

Is this a good idea? 这是一个好主意吗?

If yes: How do I create this property (or the whole file) with maven. 如果是:如何使用maven创建此属性(或整个文件)。

If no: What would be a better aproach to solve this problem? 如果不是:解决这个问题会有什么好办法?

I achieved something similar, but having the variables in the pom.xml instead in propreties files. 我实现了类似的东西,但是在pom.xml中使用变量而不是在propreties文件中。 So my properties files have variables that would be changed by Maven in packaging. 所以我的属性文件包含Maven在包装中会改变的变量。

First I defined these vars in the profiles section of the pom: 首先,我在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>

Then activated the maven procesing and filtering of resources. 然后激活maven处理和资源过滤。 So in your build section: 所以在你的构建部分:

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

Finally I can have "vars" in my properties files, configuration files. 最后,我可以在我的属性文件,配置文件中使用“vars”。 For example, In my project I have a email.properties file for configuring the emails sending. 例如,在我的项目中,我有一个email.properties文件,用于配置发送的电子邮件。 The property "sendEmail" indicates if I have to send the email (prod profile) or to print it in debug (dev profile). 属性“sendEmail”表示我是否必须发送电子邮件(prod配置文件)或在debug(dev配置文件)中打印它。 With dev profile this var will be put to false whereas with the profile prod the property will have the true value. 使用dev配置文件时,此var将被置为false,而使用profile prod时,属性将具有true值。

sendEmail=${props.email}

This not only works with properties files, also with XML (I guess with every text file) 这不仅适用于属性文件,也适用于XML(我想每个文本文件)

The contras are: 对比是:

  • Part of the configuration is dispersed along in the pom file 部分配置分散在pom文件中
  • Maven packaging lasts more (because the filtering) Maven包装持续更多(因为过滤)
  • Putting vars in XML files makes them syntaxlly erroneous (becouse the character $) 将变量放入XML文件会使它们语法错误(因为字符$)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven:从不同的配置文件产生不同的战争 - Maven: Generate different wars from different profiles 如何用不同的资源文件夹配置Maven配置文件? - How to configure Maven profiles with different resources folders? 如何使用Maven为不同的配置文件执行任务 - how to perform task for different profiles using maven Maven更改了不同配置文件的插件顺序 - Maven changes the order of plugins of different profiles 适用于不同环境的Maven配置文件-不良做法? - Maven profiles for different environments - bad practice? 如何使用不同的系统属性值执行两个完全相同的 maven 配置文件? - How to execute two exactly same maven profiles with a different system property value? Maven:在一个版本中对不同的模块使用不同的全局配置文件 - Maven: Use different global profiles for different modules in one build 如何根据不同的spring-profiles将文件写入不同的目录? - How to write files in different directories based on different spring-profiles? Maven / Netbeans具有不同的测试配置文件和环境变量 - Maven/Netbeans Have different testing profiles and environment variables 在 Maven 中为 Selenium/Cucumber 使用不同环境的配置文件属性 - Use profiles properties for different enviroments in Maven for Selenium/Cucumber
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM