为什么maven child引用BOM的版本不起作用?

时间:2017-01-23 21:56:06

标签: java maven dependency-management

我创建了这个示例POM文件,因此我不需要在每个子项目中复制的依赖项版本。我的要求是不需要为每个项目提供第三方版本。每个项目都应该从bom文件继承库版本。我基于documentation of Maven dependencies创建了示例。我的maven版本是 Apache Maven 3.2.5

我收到以下错误

[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project org.test.testapp:test-app:1.0-SNAPSHOT (/Users/skoya/workspace/test-app/pom.xml) has 2 errors
[ERROR]     'dependencies.dependency.version' for junit:junit:jar is missing. @ line 22, column 17
[ERROR]     'dependencies.dependency.version' for commons-cli:commons-cli:jar is missing. @ line 26, column 17

root bom pom file

<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>org.test</groupId>
        <artifactId>bom</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
        <name>test-dependencies-bom</name>
        <url>http://myorg.org</url>

        <properties>
                <commons-cli.version>1.3</commons-cli.version>
                <junit.version>3.8.1</junit.version>
        </properties>

        <modules>
            <module>test-dependencies</module>
        </modules>
</project>

测试依赖性/ pom.xml的

<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>org.test</groupId>
        <artifactId>test-parent-pom</artifactId>  
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>

        <name>test-parent-pom</name>
        <url>http://maven.apache.org</url>
        <parent>
                <groupId>org.test</groupId>
                <artifactId>bom</artifactId>
                <version>0.0.1-SNAPSHOT</version>
        </parent>

        <prerequisites>
                <maven>3.0.0</maven>
        </prerequisites>


        <dependencies>
                <dependency>
                        <groupId>commons-cli</groupId>
                        <artifactId>commons-cli</artifactId>
                        <version>${commons-cli.version}</version>
                </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
                        <version>${junit.version}</version>
    </dependency>

        </dependencies> 
</project>

引用bom文件的Maven项目文件

<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>
  <groupId>org.test.testapp</groupId>
  <artifactId>test-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test-app</name>
  <url>http://maven.apache.org</url>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.test</groupId>
        <artifactId>bom</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
    </dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

有两个问题:

  1. 为了导入依赖项,导入的pom项目(test-dependencies / pom.xml)应该在<dependencyManagement>部分中定义要导入的依赖项,而不仅仅是原始样本中的<dependencies>
  2. 导入该pom的项目需要导入声明依赖项的项目(test-dependencies / pom.xml),而不是父pom.xml。
  3. 我通过进行以下更改来修复此问题。

    <强>测试依赖性/ pom.xml的

    <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>org.test</groupId>
      <artifactId>test-parent-pom</artifactId>  
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>test-parent-pom</name>
      <url>http://maven.apache.org</url>
    
      <parent>
        <groupId>org.test</groupId>
        <artifactId>bom</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
    
      <prerequisites>
        <maven>3.0.0</maven>
      </prerequisites>
    
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>${commons-cli.version}</version>
          </dependency>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
          </dependency>
        </dependencies>
      </dependencyManagement>
    </project>
    

    引用bom文件的Maven项目文件

    <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>
      <groupId>org.test.testapp</groupId>
      <artifactId>test-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>test-app</name>
      <url>http://maven.apache.org</url>
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.test</groupId>
            <artifactId>test-parent-pom</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </dependency>
        <dependency>
          <groupId>commons-cli</groupId>
          <artifactId>commons-cli</artifactId>
        </dependency>
      </dependencies>
    </project>
    

    之后,我能够构建并验证导入pom的项目是否带来了预期的依赖关系:

    mvn dependency:tree
    ...
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-app ---
    [INFO] org.test.testapp:test-app:jar:1.0-SNAPSHOT
    [INFO] +- junit:junit:jar:3.8.1:compile
    [INFO] \- commons-cli:commons-cli:jar:1.3:compile
    

    请注意,它仍然成功从父pom.xml中提取属性中定义的版本号。这是构建项目的完美有效方式。它只需要我描述的两个调整。