Nexus的使用
仓库类型, nexus最重要的概念
这时候已经搭建好了Nexus,要做操作前先登录,默认用户名admin,默认密码admin123。来看看各个Repository。点击左侧Repositories。
hosted
:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
所有我们自己jar包都在这种类型的仓库中.
proxy
:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
如果本地nexus仓库中没有的jar包, 会从代理仓库中取, 就是这个意思, 当然maven中央仓库在nexus中只是代理仓库中的一个, 可以设定多个代理仓库, 比如阿里云, 比较快.
group
:仓库组,用来合并多个hosted/proxy仓库。
这个就是检索一组仓库, 取jar包, 这个貌似有缓存的, 我在后面的上传jar包时被坑了n个小时. 就是明明组里有设定其他hosted类型仓库, 就是不去检索那个仓库, 导致我自己上传的jar包在hosted仓库中, 明明有缺不检索, pom一直报错.
下面这张图, copy的作用, 是用来配置文件里粘贴仓库地址的(例如第三方jar上传的路径) 花圈的部分, maven项目发布时. 有一些第三方的jar或者公司的jar, 如果pom.xml里的version是带snapshots的, 会自动发布到maven-snapshots目录(jar名字后会带有时间戳,代表不同快照版本)
下面是配置
pom中配置:
<repositories>
<repository>
<id>maven-central</id>
<name>maven-central</name>
<url>http://192.168.1.28:8081/repository/maven-central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
Snapshot包的管理
1.先修改Maven的settings.xml文件,加入认证机制
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
2.修改工程的Pom文件
<distributionManagement>
<snapshotRepository>
<id>nexus</id>
<name>Nexus Snapshot</name>
<url>http://192.168.1.28:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<site>
<id>nexus</id>
<name>Nexus Sites</name>
<url>dav:http://192.168.1.28:8081/repository/maven-snapshots/</url>
</site>
</distributionManagement>
注意事项:
上面修改的Pom文件如截图中的名字要跟/usr/local/maven/conf/settings.xml文件中的名字一定要对应上。
3.上传到Nexus上
先配置POM文件的头部, 根据需要自己配置版本是shapshot
<groupid>com.woasis</groupid>
<artifactid>test-nexus</artifactid>
<version>1.0.0SHAPSHOT</version>
<packaging>jar</packaging>
使用mvn deploy命令运行即可(运行结果在此略过)
最后因为Snapshot是快照版本,默认他每次会把Jar加一个时间戳,做为历史备份版本。
Releases包的管理
- 与Snapshot大同小异,只是上传到私服上的Jar包不会自动带时间戳
- 与Snapshot配置不同的地方,就是工程的POM文件,加入repository配置
<repository>
<id>nexus</id>
<name>Nexus Snapshot</name>
<url>http://192.168.1.28:8081/repository/maven-releases/</url>
</repository>
- 发布版记得打包的时候需要把Snapshot去掉
<groupId>com.woasis</groupId>
<artifactId>test-nexus</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
现在就看我目前的配置文件就行, 亲测好使
- 本地maven库配置settings.xml
<settings>
<!-- 暂不知作用 -->
<pluginGroups>
<pluginGroup>org.sonatype.plugins</pluginGroup>
</pluginGroups>
<!-- 必填 -->
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/mygroup</url>
</mirror>
</mirrors>
注意: 这个地址是参照我nexus建的分组地址, 我认为分组检索比较省事, 这样写就是能够让maven直接检索我的分组里的仓库, 把需要检索的仓库都扔里就好了
下图, 把需要的仓库扔仓库组里面
<!-- 这个就是说可以免去每个项目配置发布目录的意思, 但是需要最后activeProfiles**-->
<profiles>
<profile>
<id>nexus</id>
<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>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
上传第三方jar到私服
mvn:deploy在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。
以将ojdbc14传到nexus中的thirdparty为例
我的例子: nexus3 中 将ojdbc14 传到自己 建设的 3rdLibs库中
一 配置settings.xml
因为nexus是需要登陆操作,当然可以通过配置免登陆,这是后话。
在maven的 settings.xml的<servers>
</servers>
中配置
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
如果进行deploy时返回Return code is: 401错误,则需要进行用户验证或者你已经验证的信息有误。
二 cmd输入命令
mvn deploy:deploy-file -DgroupId=com.xy.Oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=E:\ojdbc14.jar -Durl=http://localhost:9090/nexus-2.2-01/content/repositories/thirdparty/ -DrepositoryId=thirdparty
亲测: 笔记本好使的cmd命令, 我自己传的一个拼音转换jar包
mvn deploy:deploy-file -DgroupId=com.xy.pinyin -DartifactId=pinyin4j -Dversion=2.5.0 -Dpackaging=jar -Dfile=E:\pinyin4j-2.5.0.jar -Durl=http://localhost:8081/repository/3rdLibs/ -DrepositoryId=nexus
DgroupId
和DartifactId
构成了该jar包在pom.xml的坐标,项目就是依靠这两个属性定位。自己起名字也行。 Dfile
表示需要上传的jar包的绝对路径。 Durl
私服上仓库的位置,打开nexus——>repositories菜单,可以看到该路径。 DrepositoryId
服务器的表示id,在nexus的configuration可以看到。 Dversion
表示版本信息,怎样得到一个jar包准确的版本呢?
解压该包,会发现一个叫MANIFEST.MF
的文件,这个文件就有描述该包的版本信息。
比如Manifest-Version: 1.0
可以知道该包的版本了。
上传成功后,在nexus
界面点击3rd party
仓库可以看到这包。
三 一些问题
发现问题
在敲击该命令的时候,有时候看到提示需要POM文件,但上传包是不需要pom文件的。
可能原因
最大可能是你语句打错了如多了一个空格和换行,这样语句直接截断到换行前面,cmd就找它可以认识的语句执行,比如直接执行mvn,而mvn是对项目打包,是要pom文件的。
解决办法
耐心把命令重新敲一遍。还有如果是miror中配置了nexus是检索分组的地址, 例如我的配置就是检索一组仓库,那么一定注意,一定要把三方库加入到分组中去
仓库类型, nexus最重要的概念
这时候已经搭建好了Nexus,要做操作前先登录,默认用户名admin,默认密码admin123。来看看各个Repository。点击左侧Repositories。
hosted
:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
所有我们自己jar包都在这种类型的仓库中.
proxy
:代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
如果本地nexus仓库中没有的jar包, 会从代理仓库中取, 就是这个意思, 当然maven中央仓库在nexus中只是代理仓库中的一个, 可以设定多个代理仓库, 比如阿里云, 比较快.
group
:仓库组,用来合并多个hosted/proxy仓库。