我爱Java系列---【 maven依赖版本冲突的解决方法】

maven 自动按照下边的原则调解:
1、第一声明者优先原则
在 pom 文件定义依赖,先声明的依赖为准。
测试:
如果将上边 spring-webmvc 和 spring-context 顺序颠倒,系统将导入 spring-beans-5.0.2。
分析:
由于 spring-webmvc 在前边以 spring-webmvc 依赖的 spring-beans-5.0.2 为准,所以最终
spring-beans-5.0.2 添加到了工程中。
 2、路径近者优先原则
例如:还是上述情况,spring-contex 和 spring-webmvc 都会传递过来 spirng-beans,那
如果直接把 spring-beans 的依赖直接写到 pom 文件中,那么项目就不会再使用其他依赖传
递来的 spring-beans,因为自己直接在 pom 中定义 spring-beans 要比其他依赖传递过来的路
径要近。

3.锁定版本

 1 <!-- 设置编译版本为 1.8 -->
 2   <properties>
 3     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 4     <maven.compiler.source>1.8</maven.compiler.source>
 5     <maven.compiler.target>1.8</maven.compiler.target>
 6     <!--如果某个家族jar太多,用到很多版本,不容易改动,所以,声明变量,下方可以直接使用,修改这一处即可-->
 7     <spring.version>5.1.8.RELEASE</spring.version>
 8     <!--这个声明的作用只是为了以后方便查找版本号-->
 9     <mybaties.version>3.4.5</mybaties.version>
10   </properties>

4.代码演示

 

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com.itheima</groupId>
  8   <artifactId>SpringMVC_SSM</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>SpringMVC_SSM Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <!-- 设置编译版本为 1.8 -->
 17   <properties>
 18     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 19     <maven.compiler.source>1.8</maven.compiler.source>
 20     <maven.compiler.target>1.8</maven.compiler.target>
 21     <!--如果某个家族jar太多,用到很多版本,不容易改动,所以,声明变量,下方可以直接使用,修改这一处即可-->
 22     <spring.version>5.1.8.RELEASE</spring.version>
 23     <!--这个声明的作用只是为了以后方便查找版本号-->
 24     <mybaties.version>3.4.5</mybaties.version>
 25   </properties>
 26 
 27   <dependencies>
 28     <dependency>
 29       <groupId>junit</groupId>
 30       <artifactId>junit</artifactId>
 31       <version>4.12</version>
 32       <scope>test</scope>
 33     </dependency>
 34 
 35   <!-- servlet和jsp相关的-->
 36   <dependency>
 37     <groupId>javax.servlet</groupId>
 38     <artifactId>javax.servlet-api</artifactId>
 39     <version>3.1.0</version>
 40     <scope>provided</scope>
 41   </dependency>
 42   <dependency>
 43     <groupId>javax.servlet.jsp</groupId>
 44     <artifactId>javax.servlet.jsp-api</artifactId>
 45     <version>2.3.3</version>
 46     <scope>provided</scope>
 47   </dependency>
 48   <dependency>
 49     <groupId>javax.servlet</groupId>
 50     <artifactId>jstl</artifactId>
 51     <version>1.2</version>
 52   </dependency>
 53   <!-- JSTL stop -->
 54   <dependency>
 55     <groupId>taglibs</groupId>
 56     <artifactId>standard</artifactId>
 57     <version>1.1.2</version>
 58   </dependency>
 59   <!-- servlet和jsp相关的-->
 60 
 61     <!-- springmvc框架依赖 -->
 62     <dependency>
 63       <groupId>org.springframework</groupId>
 64       <artifactId>spring-webmvc</artifactId>
 65     </dependency>
 66 
 67     <dependency>
 68       <groupId>org.mybatis</groupId>
 69       <artifactId>mybatis</artifactId>
 70     </dependency>
 71 
 72     <dependency>
 73       <groupId>mysql</groupId>
 74       <artifactId>mysql-connector-java</artifactId>
 75      <!-- <version>5.1.46</version>如果这里写了,就以这里为主,下面约定的就失效,直接写最优先-->
 76     </dependency>
 77 
 78   </dependencies>
 79 
 80   <!--以下是依赖管理,它不会导包,只是用来约定版本号  通常创建父工程 约定这些东西
 81       这里一旦约定了版本号,上面导入依赖的时候就不用了导入版本号了
 82       ${spring.version},最上边properties的属性中自定义了 <spring.version>5.1.8.RELEASE</spring.version>,方便以后修改版本-->
 83   <dependencyManagement>
 84     <dependencies>
 85       <dependency>
 86         <groupId>org.springframework</groupId>
 87         <artifactId>spring-core</artifactId>
 88         <version>${spring.version}</version>
 89       </dependency>
 90 
 91       <dependency>
 92         <groupId>org.springframework</groupId>
 93         <artifactId>spring-beans</artifactId>
 94         <version>${spring.version}</version>
 95       </dependency>
 96 
 97       <dependency>
 98         <groupId>org.springframework</groupId>
 99         <artifactId>spring-tx</artifactId>
100         <version>${spring.version}</version>
101       </dependency>
102 
103       <dependency>
104         <groupId>org.springframework</groupId>
105         <artifactId>spring-context</artifactId>
106         <version>${spring.version}</version>
107       </dependency>
108 
109       <dependency>
110         <groupId>org.springframework</groupId>
111         <artifactId>spring-web</artifactId>
112         <version>${spring.version}</version>
113       </dependency>
114 
115       <dependency>
116         <groupId>org.springframework</groupId>
117         <artifactId>spring-webmvc</artifactId>
118         <version>${spring.version}</version>
119       </dependency>
120 
121       <dependency>
122         <groupId>org.springframework</groupId>
123         <artifactId>spring-expression</artifactId>
124         <version>${spring.version}</version>
125       </dependency>
126 
127       <dependency>
128         <groupId>org.springframework</groupId>
129         <artifactId>spring-jcl</artifactId>
130         <version>${spring.version}</version>
131       </dependency>
132 
133       <dependency>
134         <groupId>org.springframework</groupId>
135         <artifactId>spring-jdbc</artifactId>
136         <version>${spring.version}</version>
137       </dependency>
138 
139       <dependency>
140         <groupId>org.springframework</groupId>
141         <artifactId>spring-test</artifactId>
142         <version>${spring.version}</version>
143       </dependency>
144 
145       <dependency>
146         <groupId>org.mybatis</groupId>
147         <artifactId>mybatis</artifactId>
148         <version>${mybaties.version}</version>
149       </dependency>
150 
151       <dependency>
152         <groupId>mysql</groupId>
153         <artifactId>mysql-connector-java</artifactId>
154         <version>5.1.26</version>
155       </dependency>
156     </dependencies>
157   </dependencyManagement>
158 <!--以上是依赖管理-->
159 
160 <build>
161     <finalName>SpringMVC_SSM</finalName>
162     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
163       <plugins>
164         <plugin>
165           <artifactId>maven-clean-plugin</artifactId>
166           <version>3.1.0</version>
167         </plugin>
168         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
169         <plugin>
170           <artifactId>maven-resources-plugin</artifactId>
171           <version>3.0.2</version>
172         </plugin>
173         <plugin>
174           <artifactId>maven-compiler-plugin</artifactId>
175           <version>3.8.0</version>
176         </plugin>
177         <plugin>
178           <artifactId>maven-surefire-plugin</artifactId>
179           <version>2.22.1</version>
180         </plugin>
181         <plugin>
182           <artifactId>maven-war-plugin</artifactId>
183           <version>3.2.2</version>
184         </plugin>
185         <plugin>
186           <artifactId>maven-install-plugin</artifactId>
187           <version>2.5.2</version>
188         </plugin>
189         <plugin>
190           <artifactId>maven-deploy-plugin</artifactId>
191           <version>2.8.2</version>
192         </plugin>
193  </plugins>
194     </pluginManagement>
195   </build>
196 </project>

 

posted on 2019-07-10 18:45  少年攻城狮  阅读(1457)  评论(0编辑  收藏  举报

导航