为什么我不能从另一个配置文件激活Maven2配置文件?

为什么我不能从另一个配置文件激活Maven2配置文件?

问题描述:

我有一个用于构建Web应用程序的多模块Maven2项目.该应用程序已连接到后端服务器和数据库.在我们的环境中部署了多个服务器实例,并且还有用于开发,UAT,生产等的多个后端和数据库实例.因此,实际上,每个应用程序配置都需要以下三个坐标:

I have a multimodule Maven2 project which builds a web application. The application is connected to a backend server and a DB. There are several server instances deployed in our environment, and there are also multiple backend and DB instances for development, UAT, production, etc. So practically, each application configuration needs these 3 coordinates:

  • 前端服务器
  • 后端服务器
  • 数据库

我正在努力统一和自动化应用程序配置.在Maven中将这些不同的配置表示为配置文件很容易,也很明显.然后,我可以通过激活每个组中的一个配置文件来创建特定的配置,例如

I am working on unifying and automating the application configuration. It is easy and obvious to represent these different configurations as profiles in Maven. Then I can create a specific configuration by activating one profile from each group, e.g.

mvn -Pserver.Server1,backend.prod,db.uat clean install

这键入起来有点繁琐且容易出错-如果将特定服务器配置错误以连接到错误的DB,则价格可能会很高.解决此问题的一种明显方法是将所有有用的配置文件组合放入脚本文件.

This is a bit tedious to type and error-prone - if a specific server is misconfigured to connect to the wrong DB, the price can be high. One obvious way to fix this would be to put all useful profile combinations into script files.

但是我认为通过直接从服务器配置文件中激活必要的后端和数据库配置文件,我可以比这更聪明.服务器配置文件位于主pom中,例如

But I thought I could be more clever than that by activating the necessary back-end and DB profile directly from the server profile. The server profiles are in the main pom, e.g.

<profile>
    <id>server.myserver</id>
    <properties>
        <jboss.home>D:\Programs\jboss-4.2.1.GA</jboss.home>
        <server.name>NightlyBuild</server.name>
        <hosttobind>192.168.1.100</hosttobind>
        <servlet.port>8080</servlet.port>
        ...
        <db>dev02</db>
    </properties>
</profile>

后端和数据库配置文件位于Config子模块的pom中,例如

And the backend and DB profiles are in the pom of the Config submodule, e.g.

<profile>
    <id>db.dev02</id>
    <activation>
        <property>
            <name>db</name>
            <value>dev02</value>
        </property>
    </activation>
    <properties>
        <jdbc.address>jdbc:oracle:thin:@192.168.0.101:1521:dbdev02</jdbc.address>
    </properties>
</profile>

因此从理论上讲,由于server.myserver配置文件将db属性设置为dev02,因此这将触发子pom中db.dev02配置文件的激活.但是,这不会发生. (如果两个配置文件位于同一pom中,则也不行,顺便说一句).如果我使用

So in theory, since the server.myserver profile sets the db property to dev02, this should trigger the activation of the db.dev02 profile in the child pom. However, this does not happen. (Nor if the two profiles are in the same pom, btw). If I set the property from the command line with

mvn -Ddb=dev02 help:active-profiles

虽然配置文件已激活,但显然我没有拼错任何内容.

then the profile is activated though, so apparently I haven't misspelled anything.

我忽略了什么吗?还有其他方法可以使这项工作吗?

Have I overlooked something? Is there any other way to make this work?

我看到存在类似的问题:我可以做一个吗? Maven个人资料会激活其他人吗?
但是,恕我直言,这不是重复的-我发现我的方法行不通,我想了解原因. (我已经阅读了参考资料,但是可能忽略了一些显而易见的内容).

I see that there exists a similar question: Can I make one maven profile activate another?
However, IMHO this is not a duplicate - I see that my approach is not working and I would like to understand why. (I have read the reference, but I might have overlooked something obvious).

该功能根本不存在.属性激活器使用传入的属性,而不使用配置文件设置的任何属性(否则,如果没有一些更复杂的逻辑,它将不知道按什么顺序激活它们).

The feature simply doesn't exist. The property activator uses the incoming properties, not anything set by the profiles (as otherwise it wouldn't know what order to activate them in without some more complex logic).

您使用的解决方案具有相同的属性来激活您想一起做的事情,是最好的解决方案.我意识到这可能并不总是令人满意的-在这种情况下,您所能做的就是回到使单个配置文件尽可能简单的方式,以便您可以在命令行上以所需的方式将它们组合在一起,而不必在它们之间重复任何事情.

The solution you used, of have identical properties to activate the things you want to do together, is the best solution. I realise that may not always be satisfactory - in that case all you can do is fall back to making the individual profiles as simple as possible so that you can combine them in the ways you want on the command line, without duplicating things across them.

涉及此功能的问题是: https://issues.apache.org/jira /browse/MNG-3309
涉及属性激活的问题是: https://issues.apache.org/jira/browse /MNG-2276

The issue covering this feature is: https://issues.apache.org/jira/browse/MNG-3309
The issue covering the property activation is: https://issues.apache.org/jira/browse/MNG-2276