【问题标题】:Why is my Maven not using the defined BOM?为什么我的 Maven 不使用定义的 BOM?
【发布时间】:2015-11-26 10:36:18
【问题描述】:

我目前正在尝试将 BOM(物料清单)文件引入我的 Maven 项目,但它似乎不起作用,我不知道为什么。

这是我的bom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.private</groupId>
  <artifactId>test.bom</artifactId>
  <version>5.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <version.CoreAnnotation>5.0.0-SNAPSHOT</version.CoreAnnotation>
    <version.CoreWeb>5.0.0-SNAPSHOT</version.CoreWeb>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.private</groupId>
        <artifactId>CoreAnnotation</artifactId>
        <version> ${version.CoreAnnotation}</version>
        <type>ejb</type>
      </dependency>
      <dependency>
        <groupId>com.private</groupId>
        <artifactId>CoreWeb</artifactId>
        <version>${version.CoreWeb}</version>
        <type>war</type>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

还有更多,但我将它们排除在外,因为我认为这应该足以作为示例。

这是我的 pom(我尝试在其中包含 bom):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Core</artifactId>
    <groupId>com.private</groupId>
    <version>5.0.0-SNAPSHOT</version>
  </parent>
  <artifactId>Core-ear</artifactId>
  <packaging>ear</packaging>
  <name>Core-ear: EAR Module for private</name>

  <properties>
    <version.test.bom>5.0.0-SNAPSHOT</version.test.bom>
  </properties>


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.private</groupId>
        <artifactId>test.bom</artifactId>
        <version>${version.test.bom}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

每当我使用 ma​​ven install 时,文件夹 \target\Core-ear\ 中应该会出现在依赖项部分中定义的所有包的集合

现在的问题是:在 bom 中定义的依赖项没有出现在那里。可能是什么问题?

ps:运行mvn install -X 进行调试似乎没有帮助。

【问题讨论】:

    标签: java maven


    【解决方案1】:

    发生这种情况是因为您没有在 POM 中声明依赖项。您只声明了 &lt;dependencyManagement&gt; 部分。

    在你的 POM 中,你需要添加以下依赖,不带版本:

    <dependencies>
      <dependency>
        <groupId>com.private</groupId>
        <artifactId>CoreAnnotation</artifactId>
        <type>ejb</type>
      </dependency>
      <dependency>
        <groupId>com.private</groupId>
        <artifactId>CoreWeb</artifactId>
        <type>war</type>
      </dependency>
    </dependencies>
    
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.private</groupId>
          <artifactId>test.bom</artifactId>
          <version>${version.test.bom}</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    参考Introduction to the Dependency Mechanism:BOM 用于分解所有工件的版本,因此您无需将其添加到您的&lt;dependency&gt; 部分。

    【讨论】:

    • dependencyManagement 用于顶级 pom,使用其版本声明所有可能的依赖项。在子 poms 中,然后在所有 dependencies 中,&lt;version&gt;... 可以被排除在外。
    • @JoopEggen 是的,我没有指定它们,而是两次明确地说“没有版本”。我不确定你在说什么。
    • 抱歉,这只是关于dependencyManagement 用途的附加信息。因为似乎有错误的原因。投票 +1
    • 它告诉我,给定依赖项的版本丢失
    • @DoRe 查看我的编辑,我认为您在错误的位置添加了依赖项。它不应该在&lt;dependenciesManagement&gt;之下。
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多