首页 > 解决方案 > 在多模块项目中覆盖配置文件中的属性在 Maven 中无法正常工作

问题描述

Pom A 是 Pom B 和 Pom C 的父级。

Pom A 有一个配置文件 sample-profile,它覆盖了 Pom A 中定义的属性 <scala.version>。

<scala.version> 的默认值为 ,为此属性2.11定义的值为2.12。sample-profile

在 Pom A 的 dependencyManagement 部分中,一个依赖项的 artifactId 取决于 <scala.version> 的值。

                <dependency>
                    <groupId>com.github.scopt</groupId>
                    <artifactId>scopt_${scala.version}</artifactId>
                    <version>3.7.0</version>
                </dependency>

项目 C 依赖于 scopt_${scala.version}。项目 B 依赖于项目 C。

期望的行为是:

实际行为是:

当我运行mvn clean install -DskipTests -Psample-profilemvn dependency:tree -Psample-profile在 Project B 的目录中时, Project C 的 artifactIdscopt_2.11不是scopt_2.12

mvn help:effective-pom -Psample-profile虽然没有产生 scopt_2.11 的痕迹。

我该如何解决这个问题?

[编辑] - 最初,我使用示例配置文件从 Pom A 构建所有内容(当我这样做时,我有所需的行为,即将 scopt_2.12 作为项目 C 的依赖项)。然后在对项目 B 进行代码修改后,我只需要从 Pom B 构建(具有相同的示例配置文件)。这就是我出现意外行为的时候(即,将 scopt_2.11 作为项目 C 的依赖项)。

标签: javascalamavenmaven-3pom.xml

解决方案


我不确定真正的原因,但 maven 可能会在解析配置文件之前尝试解决依赖关系。特别是因为它们可能会影响插件,这可能会扩展 maven(请参阅maven 扩展)。

你可以试试这个:

  • 在你的 中声明这两个版本dependencyManagement,没有任何配置文件 jinx。
  • 在子项目 (B) 中,<artifactId>scopt_${scala.version}</artifactId>用作 artifactId。

这可能有效。


推荐阅读