java - maven 中 <parent></parent> 和 <dependency><dependency> 有什么区别?

标签 java maven

<分区>

在我的项目中,我总是使用 <dependency><dependency>但我可以看到<parent></parent>在一些项目中 pom.xml喜欢:

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

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

最佳答案

<parent> <dependencies> 的超集

<parent><dependencies>元素是两个截然不同的事物,但它们之间仍然存在着重要的关系。

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

groupId
version
description
url
inceptionYear
organization
licenses
developers
contributors
mailingLists
scm
issueManagement
ciManagement
properties
dependencyManagement
dependencies
repositories
pluginRepositories
build
plugin executions with matching ids
plugin configuration
etc.
reporting
profiles

As use dependencies and as use <parent>?

We can use only the first, only the second or both.
It depends really on the way which the Maven projects are designed.
Trying to enumerate all possible configurations would be long and not necessary very helpful.
So I think that you should really retain that parent is much more structuring as dependencies as it defines both more things for the children projects but it also allow not to repeat the actual configuration that you want to define in a set of projects.
So you should favor parent as you want to make inherit some child Maven projects an overall configuration and not only a list of dependencies.


Your example is perfect to illustrate the consequences on the client projects from using <parent> or dependencies as alternative.

1) With parent inheriting

Here the project inherits from the spring-boot-starter-parent pom :

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

因此,该项目将继承 dependencies 中定义的所有内容和 dependencyManagement但它也将继承自 <build> super pom 中定义的元素。

例如,您可以使用 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 项目有用的插件也将在 super 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由子项目直接继承,而不是定义 dependency<dependencyManagement><dependencies> .
这意味着这个父 pom 的 child 可以使用依赖项,但他们必须在 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 应用程序定义所有必需的依赖项,或者更直接地使用 spring-boot-dependencies 依赖于 dependencyManagementimport由于依赖管理功能,范围有办法声明它们:

<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>

关于java - maven 中 <parent></parent> 和 <dependency><dependency> 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49715759/

相关文章:

java - 如何隐藏用户在密码 JTextField 中输入的文本?

java - 远程 jvisualvm session 不支持 CPU 和分析

java - 如何在依赖项目上强制安装maven?

maven - Ivy、Maven 和 Archiva 之间有哪些异同和权衡?

java - WicketTester 与 Hibernate

Java,从证书导出公钥(X.509、Base64 .cer)

java - Java抛出InterruptedException后的中断状态

java - 贾斯珀异常 : Absolute uri cannot be resolved

Maven 发布过程替代

java - "What to share on Github"是否有任何关于 RCP 开发的指南?