【问题标题】:One profile properties are overriding with another profile properties in maven?一个配置文件属性被 Maven 中的另一个配置文件属性覆盖?
【发布时间】:2017-12-28 06:19:22
【问题描述】:

我对 Maven 配置文件有疑问。谈到细节,我有两个配置文件,例如 profile1 和 profile2。我已经为这两个配置文件以及需要由每个配置文件单独更新的模块声明了一些属性。让我们看看下面的配置,

<profiles>
    <profile>
         <id>profile1</id>
         <properties>
              <owner>ABC</owner>
         </properties>
         <modules>
            <module>module1</module>
            <module>module2</module>
         </modules>
     <profile>
     <profile>
         <id>profile2</id>
         <properties>
              <owner>XYZ</owner>
         </properties>
         <modules>
            <module>module3</module>
            <module>module4</module>
         </modules>
     <profile>
</profiles>

说到重点,profile1 属性 ABC 必须在 module1 和 module2 中更新,而 profile2 属性 XYZ 必须在 module3 和 module4 中更新。在构建应用程序时,我尝试了以下所有命令。

mvn clean install -Pprofile1,profile2
mvn clean install -P profile1,profile2

当我使用上述命令构建项目时,XYZ 在所有模块中都有更新。同样,当我使用以下命令时,ABC 会在所有 4 个模块中进行更新。

mvn clean install -Pprofile2,profile1
mvn clean install -P profile2,profile1

我的要求是只在module1和module2中更新ABC,在module3和module4中更新XYZ。你能告诉我,任何可以解决这个问题的解决方案。

注意:我什至尝试过以下命令, mvn clean install -Pprofile1 -Pprofile2 构建因目标或生命周期问题而失败。

-谢谢

【问题讨论】:

  • 首先为什么需要为不同的模块提供配置文件? (建议:永远不要为模块使用配置文件!)...
  • 我们有两个项目来构建两个具有不同属性的耳朵。这些属性必须相应地放置在需要的地方。
  • 如果你必须构建两个耳朵,你应该简单地拥有两个耳朵模块......这将简单地解决问题......
  • 你不理解我的问题 khmarbaise。构建耳朵文件不是问题。在构建耳朵文件时替换所有需要的属性值是个问题。再次阅读说明,如果您有任何疑问,请告诉我。
  • 需要替换的属性具体在哪里,目的是什么?为什么会涉及到他们的两个模块,而不仅仅是 EAR 模块?

标签: maven maven-profiles maven-module


【解决方案1】:

您的聚合器中的属性是独一无二的。因此,根据您的配置,一个配置文件会覆盖另一个配置文件。

在您的情况下,解决方案是将属性从配置文件中取出:

聚合器:

<profiles>
  <profile>
     <id>profile1</id>
     <modules>
        <module>module1</module>
        <module>module2</module>
     </modules>
 <profile>
 <profile>
     <id>profile2</id>
     <modules>
        <module>module3</module>
        <module>module4</module>
     </modules>
  <profile>
</profiles>

模块 1 和 2(无配置文件):

 <properties>
      <owner>ABC</owner>
 </properties>

模块 3 和 4(无配置文件):

 <properties>
      <owner>XYZ</owner>
 </properties>

因为在您的情况下,每个模块的属性始终相同。

然而

正如 khmarbaise 已经写的那样,您对个人资料的使用似乎有些奇怪......

【讨论】:

  • 即使我在模块级别配置了属性,它也没有替换输出文件中的正确值。您还有其他解决方案吗?
猜你喜欢
  • 2012-03-07
  • 2013-02-22
  • 2021-09-14
  • 1970-01-01
  • 2014-10-07
  • 2021-06-22
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多