仓库

Maven仓库理解和优先级
maven仓库repositories和mirrors的配置及区别详解java脚本之家

  • 本地仓库
  • 远程仓库
  • 中央仓库
    • 中央仓库就是默认的远程仓库(兜底仓库)
    • 在内置的super pom中配置

image.png

  • 镜像仓库
    • 作为中央仓库的镜像,所以mirrorOf central

image.png

  • 私服

image.png

私服也可以使用mirror

image.png

profile也可以覆盖中央仓库

image.png

仓库优先级

Maven中的localRepository(本地仓库) > Maven中的profile(远程仓库,私服?) > Pom中的repository(配置私有仓库?)> Maven中的mirror(远程仓库) > 中央仓库

私服nexus

Maven私服Nexus 3.x搭建 - 简书
maven - 图6

仓库类型:

  • 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-publicgroup类型):仓库分组,把上面三个仓库组合在一起对外提供服务,在本地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。

  1. <mirrors>
  2. <mirror>
  3. <!--This sends everything else to /public -->
  4. <id>nexus</id>
  5. <mirrorOf>*</mirrorOf>
  6. <url>http://localhost:8080/nexus-2.5/content/groups/public</url>
  7. </mirror>
  8. </mirrors>
  9. <profiles>
  10. <profile>
  11. <id>nexus</id>
  12. <!--Enable snapshots for the built in central repo to direct -->
  13. <!--all requests to nexus via the mirror -->
  14. <repositories>
  15. <repository>
  16. <id>central</id>
  17. <url>http://central</url>
  18. <releases><enabled>true</enabled></releases>
  19. <snapshots><enabled>true</enabled></snapshots>
  20. </repository>
  21. </repositories>
  22. <pluginRepositories>
  23. <pluginRepository>
  24. <id>central</id>
  25. <url>http://central</url>
  26. <releases><enabled>true</enabled></releases>
  27. <snapshots><enabled>true</enabled></snapshots>
  28. </pluginRepository>
  29. </pluginRepositories>
  30. </profile>
  31. </profiles>
  32. <activeProfiles>
  33. <!--make the profile active all the time -->
  34. <activeProfile>nexus</activeProfile>
  35. </activeProfiles>

pom配置,发布到两个指定仓库中:

  1. <distributionManagement>
  2. <repository>
  3. <id>releases</id>
  4. <name>Nexus Release Repository</name>
  5. <url>http://localhost:8080/nexus-2.5/content/repositories/releases/</url>
  6. </repository>
  7. <snapshotRepository>
  8. <id>snapshots</id>
  9. <name>Nexus Snapshot Repository</name>
  10. <url>http://localhost:8080/nexus-2.5/content/repositories/snapshots/</url>
  11. </snapshotRepository>
  12. </distributionManagement>

settings.xml中配置密码:

  1. <servers>
  2. <server>
  3. <id>releases</id>
  4. <username>admin</username>
  5. <password>admin123</password>
  6. </server>
  7. <server>
  8. <id>snapshots</id>
  9. <username>admin</username>
  10. <password>admin123</password>
  11. </server>
  12. </servers>

relativePath

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.6.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </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 文件的路径

maven - 图7

为什么要用runtime呢?直接都用compile不就行了【引用接口不引用具体实现】

A typical use case is ensuring that the client code doesn’t use a specific implementation.
确保程序没有只用指定的实现类(没有用jdbc中的实现类,而是用的接口编程)【也属于一种安全校验了】
image.png
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 - 图9

  • Optional:父项目主动引用才有
  • exclusion:父项目主动排除

    Optional和provided区别是什么?

    Difference Between true and provided
    都是不会被一起打入 jar 包。
    语义不同?

  • Optional表示功能可选项

    • 最外层可选可不选
    • 数据库驱动
  • provided表示运行时环境提供jar。
    • 表示必须,只不过不是我提供。

依赖模块打包

  1. mvn clean -U package -pl xxx-web -am -P $PROFILE -Dmaven.test.skip=$SKIP_TEST

image.png
Maven的-pl -am -amd参数学习_Jaylon Wang的专栏-CSDN博客

自动升级jar包

[1.0.0,)
设置Maven自动升级jar包_明天会更好吗的博客-CSDN博客_maven升级依赖jar

子模块版本号同步

flatten-maven-plugin
Maven多模块结构下版本号管理的正确姿势 - 掘金