【问题标题】:what is the difference between <parent></parent>and<dependency><dependency> in maven? [duplicate]maven中的<parent></parent>和<dependency><dependency>有什么区别? [复制]
【发布时间】:2018-09-17 20:06:19
【问题描述】:

在我的项目中,我总是使用&lt;dependency&gt;&lt;dependency&gt;,但我可以在某些项目pom.xml 中看到&lt;parent&gt;&lt;/parent&gt;,例如:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

所以我想知道什么时候使用它。

【问题讨论】:

  • 父 pom 本质上是当前 pom 中的包含语句。

标签: java maven


【解决方案1】:

&lt;parent&gt;&lt;dependencies&gt; 的超集

&lt;parent&gt;&lt;dependencies&gt; 元素是两个不同的东西,但它们之间存在相同的重要关系。

简单地说parent定义了当前pom的父pom,dependencies定义了当前pom的实际依赖。
父 pom 可以定义 dependencies,但也可以定义子 Maven 项目继承的许多其他东西(特别是允许配置许多东西的 dependencyManagement 元素和 build 元素)因此可以以某种方式被视为dependencies 元素的超集。
这是elements inherited from the parent pom的列表:

组 ID 版本 描述 网址 成立年份 组织 许可证 开发商 贡献者 邮件列表 单片机 问题管理 企业管理 特性 依赖管理 依赖关系 存储库 插件存储库 建造 具有匹配 id 的插件执行 插件配置 等等。 报告 简介

使用dependencies 和使用&lt;parent&gt;

我们可以只使用第一个,只使用第二个或两者都使用。
这实际上取决于 Maven 项目的设计方式。
尝试枚举所有可能的配置会很长,而且没有太大帮助。
所以我认为你真的应该保留parentdependencies 更结构化,因为它为子项目定义了更多的东西,但它也允许不重复你想在一组项目中定义的实际配置.
所以你应该支持parent,因为你想让一些子Maven项目继承一个整体配置,而不仅仅是一个依赖列表。


您的示例完美地说明了使用&lt;parent&gt;dependencies 作为替代方案对客户项目的影响。

1) 父级继承

这里项目继承自spring-boot-starter-parent pom:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

因此,该项目将继承dependenciesdependencyManagement 中定义的所有内容,但它还将继承自超级 pom.xml 中定义的 &lt;build&gt; 元素。

例如,您可以使用 Java 8 和 UTF-8 开箱即用地配置 Maven 编译器插件(您当然可以在您的子项目中重新定义):

<properties>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

此外,Spring Boot 项目可能有用的其他一些插件也将在超级 pom 中定义并由您的项目继承,例如:

<pluginManagement>
  <plugins>
     ...
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>${start-class}</mainClass>
        </configuration>
    </plugin>
    ...
  </plugins>
</pluginManagement>

请注意,父 pom 可以定义 dependencies,由子项目直接继承但不是必需的。
例如,spring-boot-starter-parent 没有定义任何直接由子项目继承的dependency,而是在&lt;dependencyManagement&gt;&lt;dependencies&gt; 中定义dependency
这意味着这个父 pom 的孩子可以使用依赖项,但他们必须在 dependencies 中明确声明。
例如:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

请注意,版本不被视为继承。

2) 没有父级继承

您必须在 Spring Boot 应用程序中定义所有必需的依赖项,或者更直接地使用 dependencyManagement 中的 spring-boot-dependencies 依赖项和 import 范围来声明它们,这要归功于依赖项管理功能:

<dependencyManagement>
        <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

但在任何情况下,您都不会从父级开箱即用配置的plugins 继承,因为您没有父级。
因此,您应该在项目的 pom.xml 中明确声明它们。

例如要定义编译器版本,使用编码和配置构建以重新打包构建的组件(使其独立可执行),您将不得不指定更多的东西:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springboot.version>1.5.2.RELEASE</springboot.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${springboot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
   <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
             <version>${springboot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>myClass</mainClass>
            </configuration>
        </plugin>      
   <plugins>
</build>

【讨论】:

    【解决方案2】:

    可以使用包装 pom 声明父 POM。它不打算分发,因为它只是从其他项目中引用。

    其中一个依赖项 pom 被声明为 jar。因此,它可以包含在项目中,因此项目可以使用它的功能。

    Maven 父 pom 可以包含几乎所有内容,并且可以继承到子 pom 文件中,例如

    • 通用数据——开发者姓名、SCM地址、分销管理等
    • 常量 - 例如版本号
    • 通用依赖项 – 所有子级通用。它与在单独的 pom 文件中多次写入它们具有相同的效果。
    • 属性 - 例如插件、声明、执行和 ID。
    • 配置
    • 资源

    【讨论】:

    • 必须分发。
    【解决方案3】:

    用例&lt;paraent&gt; 是您存储跨模块使用的工件版本和编译器设置/版本信息的地方。

    详情见下文Introduction to the POM

    【讨论】:

      【解决方案4】:

      依赖项是编译代码所需的库。这可以是您自己的代码,也可以是 Apache Commons 等库。

      父级包含信息,但没有实际构建的内容,在您的多个项目之间共享。假设您有几个相关代码的模块,并且您希望确保所有模块使用相同版本的库。然后你可以在每个模块的&lt;dependencies&gt; 部分定义这些库,但你可以在父模块的&lt;dependencyManagement&gt; 部分定义版本。

      父级 POM 可以有自己的父级,因此您可以构建一个完整的层次结构。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 1970-01-01
        • 2018-06-03
        • 1970-01-01
        相关资源
        最近更新 更多