解决jar包冲突
第一声明优先原则
- jar包坐标靠上,就是先被声明的,优先使用
路径近者优先原则
- Maven导入jar包的一些概念:
- 直接依赖: 项目中直接导入的jar包,就是该项目的直接依赖包
- 间接依赖: 项目中没有直接导入的jar包,是通过直接依赖jar包传递进来的,就是间接依赖jar包
- 路径近者优先原则,直接依赖jar包比间接依赖jar包近
直接排除法[推荐使用]:
- 直接排除法,要排除某个jar包下的依赖,可以通过
exclusions
标签
锁定jar包
- 要锁定jar包,将jar包直接复制一份,粘贴到
dependencyManagement
中
分模块构建工程
工程和模块的区别
- 工程和模块都不等于是一个完整的项目,项目完整不完整看的是代码,代码完整,就可以说是一个完整的项目。
- 工程(Project)被创建出来是独立的
- 模块(Module)一被创建出来就是有父工程的,可以直接使用父工程的资源
- 子模块之间一开始是没有联系的
- 子模块之间可以通过导入坐标来建立依赖关系
项目拆分完成后的小问题
项目拆分后web项目依赖service,service依赖dao。直接让父工程用tomcat运行可以正常使用,而web项目,用tomcat执行会发现问题
原因: 虽然导入的是其他模块的依赖,但是maven还是去本地仓库寻找,没有找到会去中央仓库寻找,都没找到时,会报错
解决方法: 将父工程在maven中安装(install
)一下,就可以将这个工程下的所有模块都放到本地仓库
当提示以下信息时,这个工程下的所有模块都已放到本地仓库1
2
3
4
5
6[INFO] Reactor Summary:
[INFO]
[INFO] maven_day02_parent ................................. SUCCESS [ 0.337 s]
[INFO] maven_day02_dao .................................... SUCCESS [ 0.937 s]
[INFO] maven_day02_service ................................ SUCCESS [ 0.053 s]
[INFO] maven_day02_web .................................... SUCCESS [ 1.369 s]
私服
私服上传jar包
需要在客户端即部署ssm_dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,在
servers
标签里配置连接私服的用户和密码。1
2
3
4
5
6
7
8
9
10<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>上传模块,将以下内容粘到pom.xml
1
2
3
4
5
6
7
8
9
10<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>然后执行deploy,就可以上传到私服(也会放到本地仓库)
从私服上下载jar包
在maven的配置文件中找到profiles
标签将这一段内容帖进去1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<!-- 下载jar包配置 -->
<profile>
<!--profile的id -->
<id>dev</id>
<repositories>
<repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
<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不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
再在最下面加上这段配置,激活上面的配置1
2
3<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
安装第三方jar包
安装到本地仓库
1 | ----进入jar包所在目录运行 |
安装第三方jar包到私服
在settings配置文件中添加登录私服第三方登录信息
1
2
3
4
5<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>在终端执行命令
1
2
3
41. 进入jar包所在目录运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
2. 打开终端直接运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=完整目录\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty