在我的项目中,我总是使用,<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>
Run Code Online (Sandbox Code Playgroud)
所以我想知道什么时候使用它.
dav*_*xxx 12
<parent>的超集<dependencies>
<parent>元素和<dependencies>元素是两个不同的事物,但它们之间仍然存在着重要的关系。
简单来说就是parent定义了当前pom的父pom,并dependencies定义了当前pom的实际依赖。
父 pom 可以定义dependencies子 Maven 项目继承的许多其他内容(特别是dependencyManagement元素和build允许配置许多内容的元素),因此可以在某种程度上被视为该dependencies元素的超集。以下是从父 pom 继承的元素
列表:
组ID 版本 描述 网址 成立年份 组织 许可证 开发商 贡献者 邮件列表 供应链管理 问题管理 ci管理 特性 依赖管理 依赖关系 存储库 插件库 建造 具有匹配 ID 的插件执行 插件配置 ETC。 报告 简介
作为使用dependencies和作为使用<parent>?
我们可以只使用第一个,也可以只使用第二个,或者两者都使用。
这实际上取决于 Maven 项目的设计方式。
尝试枚举所有可能的配置会很长,而且没有必要很有帮助。
因此,我认为您应该真正保留parent更多的结构化,因为dependencies它为子项目定义了更多内容,但它也允许不重复您想要在一组项目中定义的实际配置。
因此,parent当您希望继承某些子 Maven 项目时,您应该倾向于整体配置,而不仅仅是依赖项列表。
您的示例非常适合说明使用 <parent>或 dependencies作为替代方案对客户项目产生的后果。
1)父母继承
这里的项目继承自spring-boot-starter-parentpom :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
因此,该项目将继承dependencies和中定义的任何内容,但它也将从超级 pom.xml 中定义的元素 dependencyManagement继承。<build>
例如,您可以使用 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>
Run Code Online (Sandbox Code Playgroud)
此外,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>
Run Code Online (Sandbox Code Playgroud)
请注意,父 pom 可以定义dependencies,直接由子项目继承,但不是必需的。
例如,spring-boot-starter-parent不定义任何dependency由子项目直接继承的内容,而是dependency在<dependencyManagement><dependencies>.
这意味着该父 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>
Run Code Online (Sandbox Code Playgroud)
请注意,该版本不被视为继承的。
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>
Run Code Online (Sandbox Code Playgroud)
但在任何情况下,您都不会继承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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
374 次 |
| 最近记录: |