Maven:配置Maven从Nexus下载构件(配置私服)。

青旅半醒 2024-02-19 13:19 134阅读 0赞

当需要为项目添加Nexus私服上的public仓库时,可以按下面所示配置。

  1. <project>
  2. ...
  3. <repositories>
  4. <repository>
  5. <id>nexus</id>
  6. <name>Nexus</name>
  7. <url>http://localhost:8081/nexus/content/groups/public/</url>
  8. <releases><enabled>true</enabled></releases>
  9. <snapshots><enabled>true</enabled></snapshots>
  10. </repository>
  11. </repositories>
  12. <pluginRepositories>
  13. <pluginRepository>
  14. <id>nexus</id>
  15. <name>Nexus</name>
  16. <url>http://localhost:8081/nexus/content/groups/public/</url>
  17. <releases><enabled>true</enabled></releases>
  18. <snapshots><enabled>true</enabled></snapshots>
  19. </pluginRepository>
  20. </pluginRepositories>
  21. ...
  22. </project>

这样的配置只对当前Maven项目有效,在实际应用中,我们往往想要通过一次配置就能让本机所有的Maven项目都使用自己的Maven私服。这个时候可能会想到settings.xml文件,该文件中的配置对所有本机Maven项目有效,但是settings.xml并不支持直接配置repositories和pluginRepositories。所幸Maven还提供了Profile机制,能让用户将仓库配置放到setting.xml中的Profile中,如下所示。

  1. <settings>
  2. ...
  3. <profiles>
  4. <profile>
  5. <id>nexus</id>
  6. <repositories>
  7. <repository>
  8. <id>nexus</id>
  9. <name>Nexus</name>
  10. <url>http://localhost:8081/nexus/content/groups/public/</url>
  11. <releases><enabled>true</enabled></releases>
  12. <snapshots><enabled>true</enabled></snapshots>
  13. </repository>
  14. </repositories>
  15. <pluginRepositories>
  16. <pluginRepository>
  17. <id>nexus</id>
  18. <name>Nexus</name>
  19. <url>http://localhost:8081/nexus/content/groups/public/</url>
  20. <releases><enabled>true</enabled></releases>
  21. <snapshots><enabled>true</enabled></snapshots>
  22. </pluginRepository>
  23. </pluginRepositories>
  24. <profile>
  25. </profiles>
  26. ...
  27. </settings>

这配置中使用了一个id为nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用activeProfile元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。

上面的配置已经能让本机所有的Maven项目从Nexus私服下载构件。Maven除了从Nexus下载构件之外,还会不时的访问中央仓库central,我们希望的是所有Maven下载请求都仅仅通过Nexus,以全面发挥私服的作用。这个时候就需要借助于Maven镜像配置了。可以创建一个匹配任何仓库的镜像,镜像的地址为私服,这样,Maven对任何仓库的构件下载请求都会转到私服中。具体配置如下所示。

  1. <settings>
  2. ...
  3. <mirrors>
  4. <mirror>
  5. <id>nexus</id>
  6. <mirrorOf>*</mirrorOf>
  7. <url>http://localhost:8081/nexus/content/groups/public/</url>
  8. </mirror>
  9. </mirrors>
  10. <profiles>
  11. <profile>
  12. <id>nexus</id>
  13. <repositories>
  14. <repository>
  15. <id>central</id>
  16. <url>http://central</url>
  17. <releases><enabled>true</enabled></releases>
  18. <snapshots><enabled>true</enabled></snapshots>
  19. <repository>
  20. </repositories>
  21. <pluginRepositories>
  22. <pluginRepository>
  23. <id>central</id>
  24. <url>http://central</url>
  25. <releases><enabled>true</enabled></releases>
  26. <snapshots><enabled>true</enabled></snapshots>
  27. </pluginRepository>
  28. </pluginRepositories>
  29. </profile>
  30. </profiles>
  31. <activeProfiles>
  32. <activeProfile>nexus</activeProfile>
  33. </activeProfiles>
  34. ...
  35. </settings>

这里需要解释的是仓库及插件仓库配置,他们的id都为central,也就是说,覆盖了超级POM中央仓库的配置,他们的url已无关紧要,因为所有请求都会通过镜像访问私服地址。配置仓库及插件仓库的主要目的是开启对快照版本下载的支持,当Maven需要下载发布版或快照版构件的时候,他首先检查central,看该类型的构件是否支持,得到正面回答之后,再根据镜像匹配规则转而访问私服仓库地址。

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读