基于多个属性激活maven profile
我正在为一个项目创建一个 Maven 2 构建,并且我想出了配置文件,因为必须为不同的位置(例如柏林、巴黎、北极)和不同的环境(开发、生产)创建构建。这些是通过属性指定的。因此,对于“North Pole”“DEV”,我这样做:
-Dlocation=NorthPole -Denvironment=DEV
现在我想根据这两个属性(而不仅仅是一个)来激活我的个人资料。所以我尝试了以下操作:
<profiles>
<profile>
<id>NOrth Pole DEV</id>
<activation>
<property>
<name>location</name>
<value>NorthPole</value>
</property>
<property>
<name>environment</name>
<value>DEV</value>
</property>
</activation>
... <!-- Set some North Pole DEV specific stuff -->
</profile>
</profiles>
这不起作用,maven 期望在那里看到最多一个
元素。
请注意,我对这些属性还有其他用途,因此将其设为值 NorthPole-DEV
的单一属性 locationEnv
并不是我想要的。
那么有没有什么方法或解决方法或其他任何方式来激活基于属性组合的配置文件?
I am creating a maven 2 build for a project and I came up with profiles since the build has to be created for both different locations (say Berlin, Paris, North Pole) and different environment (Development, Production). Those are specified via properties. So for "North Pole" "DEV" I do:
-Dlocation=NorthPole -Denvironment=DEV
Now I would like to acivate my porfile based on both these properties, not just one. So I tried following:
<profiles>
<profile>
<id>NOrth Pole DEV</id>
<activation>
<property>
<name>location</name>
<value>NorthPole</value>
</property>
<property>
<name>environment</name>
<value>DEV</value>
</property>
</activation>
... <!-- Set some North Pole DEV specific stuff -->
</profile>
</profiles>
This doesn't work, maven expect to see at most one <property>
element there.
Please note I have another use for the properties as well so making it single property locationEnv
of value NorthPole-DEV
isn't what I want to have.
So is there any way or workaround or whatever else how to activate an profile based on combination of properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
恐怕没有好的解决方案可以解决您的问题(除非有我不知道的新 Maven 功能)。
理论上,您可以引入一个派生属性,其值由您列出的两个属性连接而成。然而,问题是配置文件是在 pom 中定义的属性之前评估的,因此这样的派生属性不能用于激活配置文件:-(
对于类似问题,我能想到的最佳解决方法是显式激活配置文件,并将命令行参数的不同组合放入单独的批处理/脚本文件中,以使执行更简单并避免输入错误问题。
I am afraid there is no good solution to your problem (unless there are new Maven features I am not aware of).
In theory, you could introduce a derived property whose value is concatenated from the two properties you listed. However, the problem is that profiles are evaluated before properties defined in the pom, so such a derived property can't be used to activate a profile :-(
The best workaround I could think of for a similar problem was to activate the profile explicitly, and put the different combinations of command line parameters into separate batch/script files to make execution simpler and avoid mistyping issues.
为什么不直接使用配置文件,例如:
现在您可以通过命令行激活配置文件。
why not using profile directly like:
Now you can activate the profiles by command line.
可能的解决方案
尝试此扩展: https://github.com/kpiwko/el-profile-activator -extension
这允许有这样的语法:
我自己没有尝试过,但似乎是一个不错的项目。
如何避免手动配置Maven
您需要将项目所需的两个jar放入$MAVEN_HOME/lib/ext中。不过,您可以自动配置它们。像这样:
测试简介:
Possible Solution
Try this extension: https://github.com/kpiwko/el-profile-activator-extension
This allows to have such syntax:
I did not try it myself, but seems to be a nice project.
How to avoid manual configuration of Maven
You need to put the needed two jars of the project into $MAVEN_HOME/lib/ext. You can however automize configuring them. Like this:
Tested profile:
khmarbaise 的答案对我来说似乎更优雅。
对于 Jan 的评论,您可以通过附加属性来引用该文件
例如,使用配置文件 dev,激活 North Pole,您可以参考 NorthPole-dev.xml
${location}-${env}.xml。
我不得不发表另一条回复,因为我无法向其他人的回复添加评论。 :(
khmarbaise's answer seems more elegant to me.
To Jan's comment, you can refer to the file by appending the properites
e.g. with profile dev, North Pole activated you can refer to NorthPole-dev.xml with
${location}-${env}.xml.
I had to post another reply as I'm not able to add comments to other's replies. :(
我相信你可以做这样的事情
当然,要激活配置文件,你可以在命令“-P=dev North Pole”中添加
I believe you can do something like this
Of couse, to activate a profile you can add in the command '-P=dev North Pole'
经过详尽的调查后,我发布了一个视频,其中解释了 Spring Boot 中每个环境的 Maven 配置文件的使用情况。这是一个 Spring Boot Rest 项目,它使用 Maven 配置文件处理每个环境的应用程序属性。
以下是链接:
YouTube:https://youtu.be/UbDpvh3YvDw
Github:https://github.com/carlosCharz/mavenprofilespringboot
代码片段:
应用程序参数
custom.server_url = @custom.server_url@
custom.server_port = @custom.server_port@
custom.debuggable = @custom.debuggable@
custom.image_quality = HIGH
覆盖参数
custom.server_url = api-dev.yourserver.com
custom.server_port = 80
custom.debuggable = true
让我知道如果有效的话!问候!
After an exhaustive investigation I've posted a video where I explain the usage of Maven profiles per environment with Spring Boot. That is a spring boot rest project that handle the application properties per environment using Maven profiles.
Here are the links:
Youtube: https://youtu.be/UbDpvh3YvDw
Github: https://github.com/carlosCharz/mavenprofilespringboot
Code snippet:
Application parameters
custom.server_url = @custom.server_url@
custom.server_port = @custom.server_port@
custom.debuggable = @custom.debuggable@
custom.image_quality = HIGH
Overrides parameters
custom.server_url = api-dev.yourserver.com
custom.server_port = 80
custom.debuggable = true
Let me know if it worked! Gretings!