“Maven中的就近依赖原则”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
| Jihongchang(讨论 | 贡献)  (建立内容为“1”的新页面) | Jihongchang(讨论 | 贡献)  | ||
| 第1行: | 第1行: | ||
| − | 1 | + | 当前项目的 pom.xml 继承的依赖如果在父pom中声明了版本,当前项目如果在声明引入依赖时配置了不同的版本,则当前项目的pom.xml中声明的版本生效。 | 
| + | |||
| + | 示例: | ||
| + | |||
| + | === 当前项目pom.xml === | ||
| + | <syntaxhighlight lang="xml"> | ||
| + | <?xml version="1.0" encoding="UTF-8"?> | ||
| + | <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>io.github.jihch</groupId> | ||
| + |     <artifactId>guns-v7.2.4-genrator</artifactId> | ||
| + |     <version>1.0-SNAPSHOT</version> | ||
| + | |||
| + |     <parent> | ||
| + |         <groupId>org.springframework.boot</groupId> | ||
| + |         <artifactId>spring-boot-starter-parent</artifactId> | ||
| + |         <version>2.4.0</version> | ||
| + |     </parent> | ||
| + | |||
| + |     <dependencies> | ||
| + | …… | ||
| + | |||
| + |         <dependency> | ||
| + |             <groupId>mysql</groupId> | ||
| + |             <artifactId>mysql-connector-java</artifactId> | ||
| + |             <version>8.0.31</version> | ||
| + |         </dependency> | ||
| + | …… | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | === spring-boot-starter-parent 继承的 spring-boot-dependencies pom === | ||
| + | <syntaxhighlight lang="xml"> | ||
| + | …… | ||
| + | <properties> | ||
| + | …… | ||
| + | <mysql.version>8.0.22</mysql.version> | ||
| + | …… | ||
| + | </properties> | ||
| + | …… | ||
| + |       <dependency> | ||
| + |         <groupId>mysql</groupId> | ||
| + |         <artifactId>mysql-connector-java</artifactId> | ||
| + |         <version>${mysql.version}</version> | ||
| + |       </dependency> | ||
| + | …… | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | 继承过来 mysql-connector-java 版本是 8.0.22,但因为在当前项目中又声明的版本是 8.0.31,则最后生效的还是 8.0.31 | ||
2022年12月12日 (一) 06:59的最新版本
当前项目的 pom.xml 继承的依赖如果在父pom中声明了版本,当前项目如果在声明引入依赖时配置了不同的版本,则当前项目的pom.xml中声明的版本生效。
示例:
当前项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>io.github.jihch</groupId>
    <artifactId>guns-v7.2.4-genrator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
    </parent>
    <dependencies>
……
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
……
spring-boot-starter-parent 继承的 spring-boot-dependencies pom
……
<properties>
……
<mysql.version>8.0.22</mysql.version>
……
</properties>
……
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
……
继承过来 mysql-connector-java 版本是 8.0.22,但因为在当前项目中又声明的版本是 8.0.31,则最后生效的还是 8.0.31