一:什么是依赖传递
在maven中,依赖是可以传递的,假设存在三个项目,分别是项目A,项目B以及项目C。假设C依赖B,B依赖A,那么C也就是间接的依赖A。
注:我的maven项目中依赖了
spring-webmvc.jar
,而spring-webmvc .jar,依赖于其他jar,所以我的项目就相当于间接的依赖于了其他的jar包
二:什么是依赖冲突
由于依赖传递现象的存在, spring-webmvc 依赖 spirng-beans-4.2.4,spring-aop 依赖 spring-beans-5.0.2,但是发现 spirng-beans-4.2.4 加入到了工程中,而我们希望 spring-beans-5.0.2 加入工程。这就造成了依赖冲突。
三:如何解决依赖冲突
-
使用maven提供的依赖调解原则
- 第一声明者优先原则(就是在pom.xml的位置是前还是后,
在前面的优先
) - 路径近者优先原则 (是直接依赖还是间接依赖,
直接依赖优先
)
- 第一声明者优先原则(就是在pom.xml的位置是前还是后,
-
排除依赖
-
锁定版本
2.1:第一声明者优先原则
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
spring-aop和 spring-webmvc都传递过来了
spring-beans
,但由于spring-aop的声明在spring-webmvc的前面,所以最终的spring-beans是由spring-aop传递过来的,而spring-webmvc传递过来的spring-beans是会被忽略掉的
2.2:路径近者优先原则
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
spring-aop 和 spring-webmvc 都会传递过来 spirng-beans,那如果直接把 spring-beans 的依赖直接写到 pom 文件中,那么项目就不会再使用其他依赖传递来的 spring-beans,因为直接依赖比间接依赖优先!
2.3:排除依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
<!--排除依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
使用exclusions标签将传递过来的依赖排除出去。
2.4:版本锁定
采用直接锁定版本的方法确定依赖jar包的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本为准添加到工程中,此方法在企业开发中经常使用。
- 步骤
第一步:在dependencyManagement标签中锁定依赖的版本
第二步:在dependencies标签中声明需要导入的maven坐标
<!--版本锁定-->
<dependencyManagement>
<dependencies>
<!--只是进行依赖jar包的版本锁定,这里并不会将jar包导入进来-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--这里才会将jar包导入进来-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
pom文件中使用dependencyManagement标签进行依赖jar的版本锁定,并不会真正将jar包导入到项目中,只是对这些jar的版本进行锁定。项目中使用哪些jar包,还需要在dependencies标签中进行声明。
四:分模块构建maven工程
4.1:maven工程的继承
4.2:maven工程的聚合
在maven工程的pom.xml文件中可以使用标签将其他maven工程聚合到一起,聚合的目的是为了进行统一操作。例子:如果不聚合的话那么打包的时候,需要针对每个子工程都执行
mvn package
,一旦在父工程中聚合了,那么只需要在父工程中执行该mvn package
命令,那么子工程也会跟着一起自动打包
五:nexus私服
4.1:搭建maven私服
1、下载nexus,
2、安装nexus,进入bin目录,以管理员的身份打开cmd,执行nexus.bat install
命令
3、启动nexus,进入bin目录,以管理员的身份打开cmd,执行nexus.bat start
命令
4、访问nexus,http://localhost:8081/nexus
,默认账号/密码: admin/admin123
4.2:仓库的类型(4种)
nexus默认内置了很多仓库,这些仓库可以划分为4种类型,每种类型的仓库用于存放特定的jar包
- ①hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括Releases和Snapshots两部分,Releases为公司内部发布版本仓库、 Snapshots为公司内部测试版本仓库
- ②proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
- ③group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组
- ④virtual(虚拟):兼容Maven1版本的jar或者插件
4.3:为什么要使用私服
maven私服是搭建在公司局域网内的maven仓库,公司内的所有开发团队都可以使用。例如技术研发团队开发了一个基础组件,就可以将这个基础组件打成jar包发布到私服,其他团队成员就可以从私服下载这个jar包到本地仓库并在项目中使用。
4.4:将jar包上传到私服
将项目打成Jar包发布到maven私服操作步骤如下:
①配置maven的settings.xml文件
②配置项目的pom.xml文件
③执行mvn deploy命令
1、在maven的settings.xml中的servers
标签下配置
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
2、配置项目的pom.xml
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注:这里的id要和settings.xml中的server标签下的id对应,这里的url就是将jar包上传到私服的仓库的路径,因为要访问仓库路径,所以上面settings.xml中需要配置username和password
注:关于最终项目打成jar包后是发布到哪个仓库下,是releases仓库下,还是snapshots仓库,是取决于该项目的pom.xml文件中的
<version>1.0-SNAPSHOT</version>
是什么,如果是RELEASES就发布releases仓库下,如果是SNAPSHOT
就发布到snapshots仓库下,如果没有,就是继承父工程的
3、执行mvn deploy命令
注:如果使用idea执行deploy命令没有生效,就手动在该工程的目录下执行
mvn deploy
命令即可
4.5:从私服下载jar包到本地仓库
步骤:在settings.xml文件中
1.配置nexus镜像
2.配置下载模板
3.激活下载模板
1、配置nexus镜像
<mirror>
<id>nexus</id>
<name>internal nexus repository</name>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/repositories/central/</url>
</mirror>
2、配置下载模板
<!--配置下载模板:从私服下载 -->
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
3.激活下载模板
<!--激活下载模板 -->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
测试:将本地仓库的某个jar包(junit)删除掉,然后在pom.xml中引入,我们知道此时它先会去私服中寻找,如果私服中没有,就会去中央仓库下载,然后放到私服中,以后引入该junit的jar包,直接会从私服下载,
最终下载的jar包,会存放到上面配置nexus镜像的url中http://localhost:8081/nexus/content/repositories/central/
4.6:将第三方jar安装到本地仓库
- 步骤
- 下载oracle驱动包
- 使用mvn install命令安装
mvn install:install-file -Dfile=Oracle_10g_10.2.0.4_JDBC_ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –Dversion=10.2.0.4.0 -Dpackaging=jar
4.7:将第三方jar安装到私服
有人会想,第三方jar包,我为什么还要上传到私服啊?
我直接从中央仓库下载之后,它反正会间接的放到私服这样不更加省事吗?
但是你忽略了一个问题,中央仓库虽然包含的jar包多,但也有些jar包它是没有包含进去的,
比如oracle的驱动包就是一个例子,所以我们需要手动下载oracle驱动包,然后上传到私服供团队使用!
- 步骤
- 下载oracle驱动包
- 在maven的settings.xml配置文件中配置第三方仓库的server信息
- 执行mvn deploy命令进行安装
1、在settings.xml配置文件配置第三方仓库的server信息
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
2、执行mvn deploy命令进行安装
mvn deploy:deploy-file -Dfile=Oracle_10g_10.2.0.4_JDBC_ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –Dversion=10.2.0.4.0 -Dpackaging=jar –Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
注:最后指定的
-DrepositoryId
要和settings.xml中的标签下的id值一致注:执行该命令的时候,只会将第三方jar包上传到私服,不会上传到本地仓库!
来自:虽然帅,但是菜的cxy