Maven高级

1.maven的基本概念

maven工程要导入jar包的坐标,就必须要考虑解决jar包冲突。
1.解决jar包冲突的方式一:
第一声明优先原则:哪个jar包的坐标在靠前的位置,这个jar包就是先声明的。先声明的jar包坐标下的依赖包,可以优先进入项目中
*maven导入jar包中的一些概念
直接依赖:项目中直接导入的jar包,就是该项目的直接依赖。
传递依赖:项目中没有直接导入的jar包,可以通过项目直接依赖jar包传递到项目中去,
2.解决jar包冲突的方式二:
路径近者优先原则,直接依赖路径比传递依赖路径近,那么最终项目进入的jar包会是路径近的直接依赖
3.解决jar包冲突的方式三:【推荐使用】
*直接排除法
当我们要排除某个jar包下依赖包,在配置exclusions标签的时候,内部可以不写版本号。
因为此时依赖包使用的版本和默认和本人jar包一样。
4.maven工程是可以分父子依赖关系的
凡是依赖别的项目后,拿到的别的项目的依赖包,都属于传递依赖。
**比如:当先A项目被B项目依赖,那么A项目中所有jar包都会传递到B项目中, B项目开发者,如果再在B项目中导入一套ssm框架的jar包,对于B项目是直接依赖。 那么直接依赖的Jar包就会把我们A项目传递过去的jar包覆盖掉。为了防止以上情况的出现,我们可以把A项目主要jar包的坐标锁定住,那么其他依赖该项目的项目中, 即便是有同名jar包直接依赖,也没关系

2.私服的搭建

2.1.下载nexus
Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强
大的仓库管理功能,构件搜索功能等。
下载 Nexus, 下载地址:http://www.sonatype.org/nexus/archived/
2.2.解压
解压 nexus-2.12.0-01-bundle.zip,本教程将它解压在 F 盘,进入 bin 目录:cmd 进入 bin 目录,执行 nexus.bat install
安装成功在服务中查看有 nexus 服务:
2.3 卸载 nexus
cmd 进入 nexus 的 bin 目录,执行:nexus.bat uninstall
查看 window 服务列表 nexus 已被删除。
2.4 启动 nexus
方法 1:
cmd 进入 bin 目录,执行 nexus.bat start
方法 2:
直接启动 nexus 服务
查看 nexus 的配置文件 conf/nexus.properties
配置如下:

# Jetty section
application-port=8081 # nexus 的访问端口配置
application-host=0.0.0.0 # nexus 主机监听配置(不用修改)
nexus-webapp=${bundleBasedir}/nexus # nexus 工程目录
nexus-webapp-context-path=/nexus # nexus 的 web 访问路径
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus 仓库目录
runtime=${bundleBasedir}/nexus/WEB-INF # nexus 运行程序目录

访问:
http://localhost:8081/nexus/
使用 Nexus 内置账户 admin/admin123 登陆:
点击右上角的 Log in,输入账号和密码 登陆
2.5 仓库类型
nexus
查看 nexus 的仓库:
nexus 的仓库有 4 种类型:
1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分,Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。
. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。
4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件

2.6nexus 仓库默认在 sonatype-work 目录中:
central:代理仓库,代理中央仓库
apache-snapshots:代理仓库存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/
central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件
releases:本地仓库,存储 releases构件。
snapshots:本地仓库,存储 snapshots构件。
thirdparty:第三方仓库
public:仓库组

3.将项目发布到私服

3.1 配置
第一步:需要在客户端即部署 ssm_dao 工程的电脑上配置 maven环境,并修改 settings.xml文件,配置连接私服的用户和密码 。此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。

 <server>
   <id>releases</id>
   <username>admin</username>
   <password>admin123</password>
 </server>
<server>
   <id>snapshots</id>
   <username>admin</username>
   <password>admin123</password>
</server>
releases 连接发布版本项目仓库
snapshots 连接测试版本项目仓库

==3.2第二步: 配置项目 pom.xml ==
配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号
决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为

snapshot 则上传到私服的 snapshot 仓库
<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>
注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应!

3.3 测试
将项目 dao 工程打成 jar 包发布到私服:
1、首先启动 nexus
2、对 ssm_dao 工程执行 deploy 命令:lifecycle->deploy
3.根据本项目pom.xml中version定义决定发布到哪个仓库,如果version定义为snapshot,执行 deploy后查看 nexus 的 snapshot仓库,如果 version定义为 release则项目将发布到 nexus的 release 仓库,本项目将发布到 snapshot 仓库。
2.3 在 setting.xml 中配置仓库
在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用 profile 定义仓库。

<profile> 
<!--profile 的 id-->
 <id>dev</id> 
 <repositories> 
 <repository> 
<!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
 <id>nexus</id> 
北京市昌平区建材城西路金燕龙办公楼一层 电话:400-618-9090
<!--仓库地址,即 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> 

使用 profile 定义仓库需要激活才可生效。

<activeProfiles>
 <activeProfile>dev</activeProfile>
 </activeProfiles>

4、把第三方 jar 包放入本地仓库或私服

–安装第三方jar包到本地仓库

----进入jar包所在目录运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar

----打开cmd直接运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar

–安装第三方jar包到私服

–在Maven的配置文件settings配置文件中添加登录私服第三方登录信息

<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>

----进入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

----打开cmd直接运行

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值