使用nexus3搭建私有仓库

来源:互联网 发布:广场舞扰民知乎 编辑:程序博客网 时间:2024/05/19 06:16

nexus是广为人知的搭建maven私有仓库的工具。
本文记录nexus在安装配置过程中的一些笔记。

使用docker安装nexus

docker-compose.yml配置:

version: '2'services:  nexus:    image: sonatype/nexus3:3.2.0    ports:      - 8081:8081    volumes:      - /mnt/nexus-data:/nexus-data:Z

第一次启动时会报错,提示挂载的目录写权限不足。需要修改一下挂载目录的所有者:

mkdir nexus-data && chown -R 200 /mnt/nexus-data

参考文档:
https://github.com/sonatype/docker-nexus3

在maven配置文件内,设置私有仓库的账号密码。

有两个地方可以修改maven配置。

一个是global配置。
mac下:如果是使用brew安装:/usr/local/Cellar/maven/3.3.9/libexec/conf/settings.xml
centOS下: /usr/local/apache-maven-3.3.9/conf/settings.xml
Intellij IDE:/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf/settings.xml

一个是当前用户的配置:~/.m2/settings.xml
该文件默认不存在,手动创建后, 会覆盖global的配置。
创建settings.xml文件,并将代码拷贝进去:

<settings>  <mirrors>    <mirror>      <!--This sends everything else to /public -->      <id>nexus</id>      <mirrorOf>*</mirrorOf>      <url>http://localhost:8081/repository/maven-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><servers>    <server>      <id>nexus</id>      <username>admin</username>      <password>admin123</password>    </server>  </servers></settings>

<mirror><url>标签内的地址修改成nexus服务的地址。
<servers>标签内填写nexus服务的账号密码,发布maven项目到nexus时,需要用到。
<server><id>下id需要跟<mirror><id>一致。

拉取maven项目

当拉取maven项目时,流程是:

  1. nexus检查本地是否存在该项目。
  2. 如果存在, 直接将该项目返回给客户端。
  3. 如果不存在,从maven官方仓库中拉取项目,并保存到本地。之后返回给客户端。

在nexus的web页面上,可以搜索到之前拉取的项目。


search2.png

发布项目到nexus仓库

首先在项目的pom.xml文件内,指定发布地址:

    <distributionManagement>        <repository>            <id>nexus</id>            <name>Releases</name>            <url>http://{your-nexus-ip}/repository/maven-releases</url>        </repository>        <snapshotRepository>            <id>nexus</id>            <name>Snapshot</name>            <url>http://{your-nexus-ip}/repository/maven-snapshots</url>        </snapshotRepository>    </distributionManagement>

此处注意:release版本要配置到<repository>标签内。snapshot版本配置到<snapshotRepository>标签内。

使用命令发布项目:mvn clean deploy
log显示上传发布的地址:

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ base ---[INFO] Installing /Users/Franklin/Documents/work/hldh/cloud/cloud-base-repo/pom.xml to /Users/Franklin/.m2/repository/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-SNAPSHOT.pom[INFO] [INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ base ---Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xmlDownloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 2.7 KB/sec)Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pomUploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/base-1.0-20170107.083838-2.pom (6 KB at 24.7 KB/sec)Downloading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xmlDownloaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 2.8 KB/sec)Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xmlUploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/1.0-SNAPSHOT/maven-metadata.xml (591 B at 3.0 KB/sec)Uploading: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xmlUploaded: http://nexus.store.com/repository/maven-snapshots/com/hldh/cloud/base/maven-metadata.xml (276 B at 1.1 KB/sec)

说明项目已经发布到nexus上了。
这时候在nexus的web页面上, 就能search到刚刚发布的项目了:


search.png

上传第三方jar包到nexus

发布不带pom文件的独立jar包:

mvn deploy:deploy-file -DgroupId=<group-id> \ -DartifactId=<artifact-id> \ -Dversion=<version> \ -Dpackaging=<type-of-packaging> \ -Dfile=<path-to-file> \ -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \ -Durl=<url-of-the-repository-to-deploy>

-DrepositoryId的值即为在setttings.xml里面配置的server id。

默认情况下,maven会自动为jar包创建pom文件,如果只想保留独立jar包,可以使用参数关闭这个特性:
-DgeneratePom=false

发布带有pom的jar包

mvn deploy:deploy-file -DpomFile=<path-to-pom> \ -Dfile=<path-to-file> \ -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \ -Durl=<url-of-the-repository-to-deploy>

参考:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html

使用nexus常见错误分析

  1. Request Entity Too Large

    [INFO] ------------------------------------------------------------------------[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Total time: 0.984 s[INFO] Finished at: 2017-01-08T15:20:02+08:00[INFO] Final Memory: 12M/309M[INFO] ------------------------------------------------------------------------[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact emay:emayclient:jar:4.3.4 from/to nexus (http://nexus.store.com/repository/maven-releases/): Failed to transfer file: http://nexus.store.com/repository/maven-releases/emay/emayclient/4.3.4/emayclient-4.3.4.jar. Return code is: 413, ReasonPhrase: Request Entity Too Large. -> [Help 1]

    原因:nexus服务使用了nginx做反向代理,jar包的大小超过了nginx所允许的范围。
    解决方法:修改nginx.conf配置,将client_max_body_size设置为一个较大的值:

    server { client_max_body_size 10M; listen 80; server_name localhost; location / { proxy_pass http://127.0.0.1:8000/; } }
  2. maven编译时,报错:

    Failure to find org.jfrog.maven.annomojo:maven-plugin-anno:jar:1.4.0 in http://myrepo:80/artifactory/repowas cached in the local repository, resolution will not be reattempted until the update interval of MyRepo has elapsed or updates are forced -> [Help 1]

    原因:服务器之前是使用的官方maven库拉取依赖,本地已经存在jar包,配置了nexus仓库之后,跟之前本地的jar包产生了冲突。
    解决方法:删除~/.m2/repository目录下对应的jar包。 或者干脆从新download一遍所有jar包。

    mvn clean install -U

    -U表示强制更新所有依赖

  3. 拉取到本地的第三方库,只有lastUpdated文件,却不见pom和jar文件:
    本地报错:

    [WARNING] The POM for xxx.jar is missing, no dependency information available

    我一直以为是nexus有bug,代理maven中央仓库时出错。
    最后找了好久,发现原来是这个库的groupId改了,而且它还删除了maven中央仓库的groupId对应的包,maven找不到对应的pom和jar包,就只会创建lastUpdated文件。
    这次更坚信了nexus的代理机制是很健壮的,出问题一般都是自己本地的问题。

nexus官方文档:
http://books.sonatype.com/nexus-book/reference3/maven.html#maven-sect-single-group

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小兔子生下来母兔子不喂奶怎么办 11个月的宝宝肚子有小白虫怎么办 电焊把脸烤了痒的不行怎么办? 还没满月的宝宝吐奶怎么办 周岁宝宝发烧腹泻吃药老吐怎么办 生完宝宝后肚皮很黑怎么办 两个月宝宝抵抗力差总生病怎么办 两岁宝宝只会说几个简单的词怎么办 两岁宝宝对绘本不感兴趣怎么办 一岁十个月宝宝不爱吃饭怎么办 孩子上三年级学习太差应该怎么办 mac系统打数字出显符号怎么办 我太粗心了工作中总犯错怎么办 四年级的孩子数学计算总粗心怎么办 小孩做作业拖拉的很厉害怎么办 二年级孩子做作业太粗心怎么办 发现孩子做作业时玩手机怎么办 高三孩子考试时慌乱老粗心怎么办 助班面试你的学生不喜欢你怎么办 上三年级的小孩不愿做作业怎么办 快一年了孩子上幼儿园总哭怎么办 宝宝吃多了吐了很多怎么办 一岁宝宝只吃母乳不吃辅食怎么办 孩子平常考得好正式考不好怎么办 平常成绩好期末却考不好怎么办 初中生去国外上学国内的学校怎么办 孩子在学校不受老师的重视怎么办 孩子上二年级了不爱学习怎么办 接别人的班被学生讨厌怎么办 孩子在学校老师打孩子家长怎么办? 宝宝不听话我老忍不住打他怎么办 两岁宝宝断母乳不愿意喝奶粉怎么办 2岁半的宝宝叛逆不听话怎么办? 2岁半宝宝说什么不听话怎么办? 误把脱毛膏当牙膏用了刷牙怎么办 儿子1岁5个月了不说话怎么办 两岁小儿不会说话和智商低怎么办 宝宝不会说话教他说他不愿意怎么办 25个月宝宝不愿意学说话怎么办 老师说小孩在幼儿园老是说话怎么办 在外留学想领养一个外国小孩怎么办