java - 当另一个配置文件被激活时自动激活一个配置文件

标签 java maven

我知道不可能“链接”个人资料声明。 可以在命令行中使用系统属性(如 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 是默认配置文件,即使没有命令行参数也应该可以做到这一点。我尝试定义一个值,但它不起作用,因为激活属性仅适用于系统属性。

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

谢谢

最佳答案

Maven 不会在那里帮助您,您可以在配置文件之间没有依赖关系或继承,另请参阅 my recent answer to a related question .

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

来自 Introduction to Build Profiles :

A profile can be triggered/activated in several ways:

  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files

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

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

关于java - 当另一个配置文件被激活时自动激活一个配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5340701/

相关文章:

java - 为什么 Maven 不运行测试用例?

java - 构造函数 PrintWriter(BufferedWriter) 未定义

java - HashMap 改变 Values 对象

java - Java 库的版本信息,可在运行时访问

java - Geotools lib 突然从存储库中消失

java - Maven 程序集插件未创建带有目录条目的 tar 文件?

java - jar list 中的注释行

java - 无法编译和运行Android Market API

java - 当我将 Final 变量设置为 4 时,如何确保删除顶部 4 行而不是底部 4 行?

maven - 两个相互依赖的 Maven 项目