java - Maven 中的导入和依赖管理

标签 java maven dependency-management

我正在学习 Maven 中的“导入”范围,并在下面做了一个示例项目,

Project_1 POM 文件:

    <dependencyManagement>  
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies> 
    </dependencyManagement>
</project>

project_2 POM 文件:

<parent>
<groupId>com.sample</groupId>
  <artifactId>project1</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>    
...
...
<dependencyManagement>  
      <dependencies>
        <dependency>
          <groupId>com.sample</groupId>
        <artifactId>project1</artifactId>
        <version>1.0-SNAPSHOT</version>
           <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

但是,这会引发一个错误,指出 JUnit 包在 Project2 中不可用。当我从 project_1 POM 文件中删除 dependencyManagement 标签时。一切正常。 但根据 maven docs ,

This scope is only supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

我已经按照文档中的说明完成了,这里出了什么问题?

最佳答案

您似乎正在尝试使用 Material list (BOM) POM 并将其导入。

你的 Project_1在这种情况下是 BOM POM,并且您将项目的所有依赖项包含在 <dependencyManagement> 中元素。看来您的做法是正确的。

然而,要在您的项目中导入 BOM POM,您需要 <dependencyManagement><dependencies> .

例如,您的 Project_2 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>project1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencyManagement>
        <dependencies>
            <!-- Imports the bill-of-materials POM. -->
            <dependency>
                <groupId>com.sample</groupId>
                <artifactId>bom</artifactId>
                <version>1.0-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Add a dependency from the BOM POM.
        Note: no version or scope is required, it's inherited from the BOM POM! -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

下面是上述示例的 BOM POM 定义:

<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>com.sample</groupId>
    <artifactId>bom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

documentation about this在 Apache Maven 网站上(搜索“bom”)。

更新:

BOM POM 和父 POM 相似但不同。 BOM POM 纯粹用于将依赖项导入项目。父 POM 用于多模块项目,允许您定义 Maven 坐标、插件和依赖项,这些将由使用父 POM 的所有模块继承。

这是一个使用从父 POM 继承的依赖项的示例。请注意,此处包含的几个元素在 BOM POM 中没有意义。

<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>
        <groupId>com.sample</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>project1</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- Inherited from the parent POM. -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

这是上面例子的父 POM 定义:

<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>com.sample</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--Global dependencies...-->
    </dependencies>

    <build>
        <pluginManagement>
            <!--Plugins...-->
        </pluginManagement>
    </build>
</project>

关于java - Maven 中的导入和依赖管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44740355/

相关文章:

php - 当作为依赖项包含在 Composer 中时,自动将文件包含在包中

matlab - matlab 脚本中是否使用了某些 matlab 例程?

c# - VLC Media Player 如何实现 HTTP Streaming?

java - Spring Java 配置用于在 stomp 端点上自定义 Websocket 握手

java - 我可以更新 ApplicationModule 并在每次注入(inject)时获取更新的值吗

java - Maven Cucumber 报告多个 JSON 文件

java - 整数超出 2147447602 值

java - 尝试使用 Maven 将 war 部署到 tomcat 时得到 403

maven - 如何管理javascript/图像依赖

layout - Dart 包布局