仓库
Maven仓库理解和优先级
maven仓库repositories和mirrors的配置及区别详解java脚本之家
- 本地仓库
- 远程仓库
- 中央仓库
- 中央仓库就是默认的远程仓库(兜底仓库)
- 在内置的super pom中配置
- 镜像仓库
- 作为中央仓库的镜像,所以mirrorOf central
- 私服
私服也可以使用mirror
profile也可以覆盖中央仓库
仓库优先级
Maven中的localRepository(本地仓库) > Maven中的profile(远程仓库,私服?) > Pom中的repository(配置私有仓库?)> Maven中的mirror(远程仓库) > 中央仓库
私服nexus
仓库类型:
- proxy:代理
- hosted:宿主仓库,一般用来存放公司自己的jar包;
- Group:集合仓库
仓库:
1)maven-central(proxy类型):maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
2)maven-releases(hosted类型):私库发行版jar
3)maven-snapshots(hosted类型):私库快照(调试版本)jar
4)maven-public(group类型):仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。
Public Repositories下的仓库:
1)3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库,即第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去;
2)Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库
3)Central: 用来代理maven中央仓库中发布版本构件的仓库
4)Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库
5)Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库
6)Releases: 内部的模块中release模块的发布仓库,用来部署管理内部的发布版本构件的宿主类型仓库;release是发布版本;
7)Snapshots:发布内部的SNAPSHOT模块的仓库,用来部署管理内部的快照版本构件的宿主类型仓库;snapshots是快照版本,也就是不稳定版本
私服nexus配置
Maven提高篇系列之(三)——使用自己的Repository(Nexus) - 无知者云 - 博客园
配置一个无用的仓库,activeProfile开启nexus,最终都走nexus mirror。
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8080/nexus-2.5/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
pom配置,发布到两个指定仓库中:
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8080/nexus-2.5/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8080/nexus-2.5/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
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>
</servers>
relativePath
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Maven parent.relativePath默认值为../pom.xml,设定一个空值将始终从仓库中获取,不从本地路径获取,如
snapshot 和 release 有啥区别
- snapshot尽管版本号不变,每次都要从新获取新的jar文件,而不是使用本地上的jar。
- release不去获取新jar,即便远程仓库修改了jar。
使用 -U 参数强制 maven 现在最新的快照构建
mvn clean package -U
maven scope
- compile
- 会被打包
- provided
- servlet API
- 不传递,不打包(运行时也不要)
- runtime
- JDBC API
- 运行时才需要
- test
- 测试时需要
- system
- 显式的提供一个对于本地系统中JAR 文件的路径
为什么要用runtime呢?直接都用compile不就行了【引用接口不引用具体实现】
A typical use case is ensuring that the client code doesn’t use a specific implementation.
确保程序没有只用指定的实现类(没有用jdbc中的实现类,而是用的接口编程)【也属于一种安全校验了】
java - When would I need maven dependency with runtime scope - Stack Overflow
为什么servlet-api是provided?
程序会将依赖jar拷贝到WEB-INF/lib中。而tomcat提供的servlet-api在$CATALINA_HOME/lib中。
tomcat的类加载顺序,先加载WEB-INF/lib,后加载$CATALINA_HOME/lib。
类加载
程序自带的Servlet-api可能和tomcat版本不匹配,所以用tomcat的servlet-api,而不是用程序提供的servlet-api。(如果自带的jar能被覆盖,那也不会有问题)
Optional
- Maven optional关键字透彻图解 - 日拱一兵 - 博客园
- 漫画:工作这么多年,你居然不知道 Maven中 Optional 和 Exclusions 的区别? - 知乎
- maven 中 provided 与 optional 的区别 - 知乎
- Optional:父项目主动引用才有
-
Optional和provided区别是什么?
Difference Between true and provided
都是不会被一起打入 jar 包。
语义不同? Optional表示功能可选项。
- 最外层可选可不选
- 数据库驱动
- provided表示运行时环境提供jar。
- 表示必须,只不过不是我提供。
依赖模块打包
mvn clean -U package -pl xxx-web -am -P $PROFILE -Dmaven.test.skip=$SKIP_TEST
Maven的-pl -am -amd参数学习_Jaylon Wang的专栏-CSDN博客
自动升级jar包
设置Maven自动升级jar包_明天会更好吗的博客-CSDN博客_maven升级依赖jar
子模块版本号同步
flatten-maven-plugin
Maven多模块结构下版本号管理的正确姿势 - 掘金