Maven 使用 Nexus 内部库 代理

反正任由总理怎么强调,在中国的当前的网络环境下,中央库的访问速度总是令人心碎。建一个nexus内部库可以建立缓存,只要有人通过它下载了相关的maven依赖,那么别人需要时可以马上从本地网络的服务器上返回而不需要在从中央库去下了。内部也可以将jar发布到本地服务器上便于队友共享使用。有时候队友只是想直接用你编译好的包,而不想花时间去配什么本地环境。所以这比单单代码库又方便快捷了许多。

1. nexus服务器

基本开箱可用,改一下管理员密码即可。已经预先为用户把中央仓库以代理的方式加进来了,改一下打开下载索引的配置即可。不过因为糟糕的网络环境,这通常是成功不了的。

2. maven的settings.xml文件

可以从maven安装目录里复制一份到用户目录下得.m2文件夹下作为用户settings(确保eclipse中的maven插件中settings文件路径正确),也可以直接在里面改作为global settings

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
  </pluginGroups>
<proxies> </proxies> <mirrors> <mirror> <id>local-nexus</id> <mirrorOf>*</mirrorOf> <name>database lab local maven repo</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>dev</id> <repositories> <repository> <id>central</id> <name>local-maven-main-repository</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>local-maven-plugin-repository</name> <url>http://10.214.208.144:8080/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings>

profile中的的repository最好把pluginRepositories也写上。不过感觉只配个mirror也可以了,不用配profiles和activeProfiles。不过可能没有遇到特殊的情况,因为profile里把releases和snapshots两选项打开了,真是搞不明白,配置够烦得。

3. eclipse

新建一个maven项目,然后如果eclipse有对pom.xml打了个叉,提示有什么依赖包下载不到什么的,先不用管它。

到项目所在的目录执行:

mvn clean

mvn package

第一个先做个清理,第二个进行一次打包,相应的maven需要的插件就会去下载了,一般成功的话会显示BUILD SUCCESS。然后多刷新eclipse的pom.xml文件就可以了,或者关闭eclipse再打开,那些错误应该就没了。

 一些包无法下载

1. nexus刚刚运行的时候,有些包可能无法下载,在管理界面里可以对比browse index和browse remote里面的项,有些只有在remote里有本地index里还没有。

另外实在碰到一些奇葩包无法下载,可以把本地maven库缓存里的包全删了(.m2/repository),然后mvn clean再来就行了。最好是等服务器搭好,过个一天再尝试。可以把repo.spring.io这个仓库加进来,里面有很多东西。

2. 在库的配置选项Routing页中把Discovery关闭(去掉选择)

默认情况下是打开的,但因为好像有的库中会有一个prefix文件,

点可以查看prefix文件内容:

## repository-prefixes/2.0
#
# Prefix file generated by Sonatype Nexus
# Do not edit, changes will be overwritten!
/archetype-catalog.xml.sha1
/archetype-catalog.xml.md5
/monetdb/monetdb-jdbc
/archetype-catalog.xml
/pentaho/json
/org/json

打开discovery后不在prefix里的包好像都下载不到,在浏览器中直接输入对应地址的话会出现类似以下的错误:

404 - ItemNotFoundException

Request is marked as local-only, remote access not allowed from M2Repository(id=pentaho-snapshot)

 org.sonatype.nexus.proxy.ItemNotFoundException: Request is marked as local-only, remote access not allowed from M2Repository(id=pentaho-snapshot)

把discovery禁用的话,就不会按照prefix文件的限定,可以下载任何库里能看到的包

 

原文地址:https://www.cnblogs.com/lailailai/p/4521712.html