# maven版本强行一致方法

#### 父 POM 管版本，子 POM 管依赖

- 父 POM 只放 <dependencyManagement> 或 BOM，不声明实际依赖

```xml

	<properties>
		<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
	</properties>

	<!-- 依赖声明 -->
	<dependencyManagement>
		<dependencies>
			<!-- SpringBoot的依赖配置 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${spring-boot.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	<dependencyManagement>
```

- 子 pom.xml 只写 groupId + artifactId（甚至 scope/classifier），不写  version

```xml

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 版本由父 POM 决定 -->
  </dependency>
</dependencies>


```

#### 使用强依赖插件 maven-enforcer-plugin

插件发布地址：<https://central.sonatype.com/artifact/org.apache.maven.plugins/maven-enforcer-plugin>

```xml

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-enforcer-plugin</artifactId>
                <version>3.6.1</version>
				<executions>
					<execution>
						<id>convergence</id>
						<goals><goal>enforce</goal></goals>
						<configuration>
							<rules>
								<dependencyConvergence/>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>



```

执行验证和编译，此时会提示一堆错误，根据错误提示修复依赖冲突

```shell

# 执行验证
mvn validate

# 执行编译
mvn clean install -DskipTests



```

#### 错误太多修复不过来


可以使用vscode打开项目，在cline窗口输入提示词，让AI帮忙修复

- 指令一（速度快，可能有错误）：执行mvn clean validate ，并根据控制台的错误进行问题修复，依照父pom管版本，子pom管依赖原则修复。

- 指令二（执行慢，但是可靠）：执行mvn clean install -DskipTests ，并根据控制台的错误进行问题修复，依照父pom管版本，子pom管依赖原则修复。
- 指令三：检查一下项目pom文件，按照 父 POM 管版本，子 POM 管依赖进行调整