【问题标题】:automatically activates a profile when another is actived激活另一个配置文件时自动激活一个配置文件
【发布时间】:2011-07-17 11:26:23
【问题描述】:

我知道“链接”个人资料声明是不可能的。 可以使用命令行中的系统属性(例如 mvn -Dvar=value )做我想做的事情(但不完全)。 然后使用这个:

  <activation>
    <property>
      <name>var</name>
      <value>value</value>
    </property>            
  </activation>

或使用 -P profile1, profile2 同时激活多个配置文件

事实上,我有 4 个配置文件,我想创建第五个配置文件,其中包含所有 aspectj 部分(插件、依赖项等),只有在配置文件 1、2 或 3 被激活时才应该激活它。像这样我没有多余的插件和依赖声明。

<profile>
  <id>profile1</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <build>
    ...     
  </build>
</profile>

<profile>
  <id>profile2</id>
  <build>
    ...     
  </build>
</profile>

<profile>
  <id>profile3</id>
  <build>
    ... 
  </build>
</profile>
<profile>
  <id>profile4</id>
  <build>
    <plugins>
      <plugin>
        ...
      </plugin>
    </plugins>
  </build>
</profile>

<profile>
  <id>aop</id>
  <build>
    <plugins>
      <plugin>
        ...
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      ...
    </dependency>
    <dependency>
      ...
    </dependency>
  </dependencies>
</profile>

问题在于配置文件 1 是默认配置文件,甚至应该可以在没有命令行参数的情况下执行此操作。我尝试定义一个值,但它不起作用,因为激活属性仅适用于系统属性。

我错了吗?还有其他方法吗?

谢谢

【问题讨论】:

标签: java maven


【解决方案1】:

Maven 帮不了你,你可以在配置文件之间没有依赖或继承,另见my recent answer to a related question

您可以显式打开或关闭配置文件,但无法定义它们之间的任何关系。 Maven 不是 ant,这通常是一件好事,但这是我认为它应该更像 ant 的情况之一。

来自Introduction to Build Profiles

可以触发/激活配置文件 在几个方面:

  • 明确
  • 通过 Maven 设置
  • 基于环境变量
  • 操作系统设置
  • 存在或丢失文件

这些都无助于定义配置文件之间的任何关系(不能使用项目属性,因为配置文件是在构建项目时评估的)。所以你唯一的选择是

  • 遵守限制并使用-P 语法明确激活您需要的配置文件
  • 编写一个封装了maven调用并自动添加缺失配置文件的shell脚本
  • 构建您自己的 DefaultProfileSelector 替代品,如 my previous answer 中所述

【讨论】:

  • 嗨,肖恩,感谢您的回复。我将寻找 DefaultProfileSelector 的替代品。他们应该让我们在 maven (${}) 中定义一个属性,我们可以在激活部分或使用配置文件的继承或聚合机制中检查该属性。
猜你喜欢
  • 2014-10-29
  • 2010-10-30
  • 2011-01-15
  • 1970-01-01
  • 2011-11-12
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多