使用nexus搭建maven私服
搭建nexus私服原因:
- 公网jar包下载代理。有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找一台有外网权限的机器,搭建nexus私服。开发人员连到这台私服上就可以通过这台搭建了nexus私服访问maven的远程仓库。
- 共享下载的jar包。通过私服下载的jar包会存储在私服中,不必重复下载。
- 下载nexus的war包:https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-04.war
- 将nexus的war包放到tomcat容器中,并启动tomcat
-
通过http://ip:port/nexus访问nexus页面,nexus默认创建了maven中央库的代理,如果需要添加其它的maven代理库需要创建新的proxy repository 。注:nexus初始管理员账号和密码为:admin / admin123
-
创建资源库组。一个资源库组中包含多个资源库,nexus会根据资源库顺序下载jar包。注:在创建repository时,添加nexus默认中央库需要注意顺序。
资源库引用方式:
<repositories> <repository> <id>nexus</id> <name>nexus repository</name> <url>http://ip:port/nexus/content/groups/repositorygroup</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Team Nexus Repository</name> <url>http://ip:port/nexus/content/groups/repositorygroup</url> </pluginRepository> </pluginRepositories>注:如果在项目中直接使用pom.xml中配置的资源库,需要首先将maven本地配置文件setting.xml中删除maven默认的资源库配置。
5. maven发布配置
在pom.xml中配置发布的nexus地址,如下:
在pom.xml中配置发布的nexus地址,如下:
<distributionManagement> <repository> <id>releases</id> <name>Nexus Release Repository</name> <url>http://ip:port/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshot</id> <name>Nexus snapshot Repository</name> <url>http://ip:port/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
在maven本地仓库setting.xml中配置nexus发布库对应的用户信息。server标签id对应的内容和repository标签id和内容相同。执行mvn deploy命令后,maven会将项目发布到nexus中。配置如下:
<servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshot</id> <username>admin</username> <password>admin123</password> </server> </servers>注:
- 对于经常变动的库项目最好使用快照版本,即在原有版本号的基础上加上“-SNAPSHOT”,例如:0.0.2-SNAPSHOT。在发布项目时,maven会自动将快照版本发布到快照库;如果nexus中快照版本发生变化,maven也会自动下载最新的版本快照项目。
- nexus的配置文件位于WEB-INF\classes\nexus.properties,其中nexus-work指定了nexus配置及下载文件的目录,在移动nexus时为了避免重复下载,最好将nexus的配置目录也做移动。