Maven 解决依赖冲突有以下几种方式:
排除冲突依赖:在 pom.xml 文件中使用<exclusions> 标签来排除特定的依赖项。例如:<dependency> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>group2</groupId> <artifactId>artifact2</artifactId> </exclusion> </exclusions></dependency>这样就会排除掉 group1:artifact1 依赖中的 group2:artifact2。
引入指定版本的依赖:使用 Maven 的强制依赖机制,即在 pom.xml 文件中将特定的依赖项设置为强制版本。例如:<dependency> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <version>1.0</version></dependency><dependency> <groupId>group2</groupId> <artifactId>artifact2</artifactId> <version>2.0</version></dependency><dependency> <groupId>group3</groupId> <artifactId>artifact3</artifactId> <version>3.0</version> <exclusions> <exclusion> <groupId>group2</groupId> <artifactId>artifact2</artifactId> </exclusion> </exclusions></dependency>在上述例子中,group3:artifact3 依赖排除了 group2:artifact2,因此 Maven 会使用强制版本的 group2:artifact2。
使用 Dependency Management:在 pom.xml 文件的<dependencyManagement> 标签下,可以定义项目中所有依赖项的版本。这样可以统一管理所有的依赖版本,避免冲突。例如:<dependencyManagement> <dependencies> <dependency> <groupId>group1</groupId> <artifactId>artifact1</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>group2</groupId> <artifactId>artifact2</artifactId> <version>2.0</version> </dependency> </dependencies></dependencyManagement>这样在项目中引入这些依赖时,不需要指定版本号,Maven 会自动使用 <dependencyManagement> 中定义的版本。
需要根据具体情况选择合适的解决冲突的方式。

