本文主要介绍,使用maven在nexus私服上传和下载
关于nexus的安装,请参考上篇文章 Windows安装nexus oss3.x
上传jar包到Nexus私服
查看官方文档配置说明 : Configuring Apache Maven
Nexus官方的pdf书籍 : Repository Management With Nexus
修改maven主目录 conf/settings.xml
配置文件.添加nexus认证的用户名和密码配置信息
<server>
<id>nexus-releases</id>
<privateKey>admin</privateKey>
<passphrase>admin123</passphrase>
</server>
<server>
<id>nexus-snapshots</id>
<privateKey>admin</privateKey>
<passphrase>admin123</passphrase>
</server>
在要发布的Project中的 pom.xml
文件中添加如下内容
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Releases</name>
<url>http://localhost:8081/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Snapshot</name>
<url>http://localhost:8081/repository/mavensnapshots</url>
</snapshotRepository>
</distributionManagement>
注意 : settings.xml
文件中的配置的id名称要和上面的id名称一致
Nexus2可以通过管理界面来上传jar包到私库中,而最新的Nexus3却找不到了上传界面,只能通过以下方式来发布到私库。
Maven配置
在项目中使用 mvn deploy
就可以打包发送到nexus私服上
问题
以上过程中可能会出现的问题 : Return code is 400:
因为Nexus中Releases仓库默认的Deployment Policy是Disable Redeploy
,部署构件到Releases仓库中可能会出现400的错误
解决办法 : 将Deployment Policy 选项改为 Allow Redeploy
,流程如下图:
具体其他的问题可以参考下面的链接(讲得非常详细):
http://www.javatang.com/archives/2010/01/23/4518375.html
小结
Snapshot是快照版本,默认每次会把Jar加一个时间戳,做为历史备份版本。
Release上传到私服上的Jar包不会自动带时间戳
下载jar包到本地仓库
下载的jar包可能是本公司的或者中央仓库的,所以使用group
仓库来配置
在maven目录下的 conf/settings.xml
下,配置模版
添加镜像
<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://localhost:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://localhost:8081/repository/maven-public/</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>