1在pom导入依赖的GAV标签下方可添加标签<scope></scope>,该标签确定了jar包(仓库)的生命周期. 分别有:complie(全局依赖),test(测试依赖),runtime(运行时依赖),provide(参与编译和测试,但发布时不被打包),system(类似provide,但在本地磁盘查找仓库). 例如:junit包可设定为test依赖
2在setintg设置文件内可设定阿里云仓库,作为中央仓库的镜像仓库,可以提高大陆jar包的下载速度)(镜像概念:代替从外网下载的内网网站)
3maven的三个特性:
1)传递性:①项目需要导入A, 若A需要B , 则项目会自动导入B (好处:方便 缺点:可能会造成jar冲突)
maven的传递原则是短路径优先(层级少的不同版本相同jar包优先)
解决办法:可用<exclusions> <exluesion> <G></> <A></> </> </>去除导入冲突的jar包
②传递依赖范围: 项目A中依赖B库 , B库依赖C库, 则A对B是第一直接依赖, B对C是第二直接依赖, A对C是传递性依赖
例如:第一直接依赖是conplie(测试,编译,运行), 第二直接依赖是runtime(测试,运行) 则A对C的传递性依赖为runtime(只有A项目发布后需要运行使用的jar包才会被导入,B对C是runtime需编译,故A对C是runtime)
同理: 第一直接依赖是conplie(测试,编译,运行), 第二直接依赖是provide(测试,编译) 则A对C的传递性依赖为无
2)聚合:项目中有多个模块,每个模块都是一个maven项目,在一个单独的模块中汇总其他模块,对该模块进行操作会同步到其他模块
聚合项目(模块)不需要src,只需要pom文件作为一个操作的入口即可,在pom文件内配置:
<G><A><V>素质三连
<packing>pom</packing>
<Models>
<Model>../模块1</Model>
<Model>../模块2</Model>
</Models>
上述被添加的模块1,模块2在聚合项目被编译,测试,打包时也会被进行该操作
3)继承:重复的pom配置可用子父继承来配置,减少操作
①父项pom: <dependoces>
<dependency>
<G><A><V>
</dependency>
</dependoces>
子项pom:<parent>
<G>父项groupId<G>
<A>父项arrtitactId</A>
<V>父项version</V>
</parent>
②若父项在<dependices>标签外在加个<dependencyManagement>标签则子项可继承,可不继承(继承的话使用父项版本号,为了统一版本,且子项可不写<Version>标签)
4私服私服是架设在局域网的特殊的远程仓库,在私服内部署的jar包可以被项目内其他开发人员使用(而不必每个人在中央仓库各下各的)
①软件:nexus(Windows版以bundle结尾) 解压安装后用cmd命令行在nexu的bin目录下执行nexus install 和 nexus start. 可以在控制面板管理nexus的开启与关闭. 访问:htttp://localhost:/8081/nexus(右上角登录 账号:admin 密码:admin123) 验证是否安装成功
②在网页内管理私服
1)仓库Type hosted:仓库所有内容就在本地; proxy:代理仓库,连接远程仓库 group:仓库组,将其他仓库放入其中,平时项目就使用该仓库
2)导入jar到私服: 选中仓库-Artifact Upload, GAV选项勾选GAV Parameters,在下方定义GAV坐标.再在Filename内选择需要上传的jar后点击Add attrifact,最后upload
③maven配置私服
1)使maven可以从私服上获取构件:确认group仓库组id和name,确认其procider栏是Maven2,确认其地址path
2)先在maven的setting中设置私服地址和认证信息
<server>
<id>nexus-public</id>
<username>admin</username>
<password>admin123</password>
</server>
再设置profile,其id要和<Server>内的ID一致,其地址为group仓库组的地址
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus-public</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-public</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
最后对nexu激活jdk-1.8
<activeProfiles>
<activeProfile>jdk-1.8</activeProfile>
<activeProfile>nexus</activeProfile>
</activeProfiles>
③maven部署到私服