[英]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: 对比是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.