Maven私服Nexus的搭建

私服存在的合理性
Maven中的依赖是从服务器仓库中下载的,Maven的仓库只有两大类:
1) 本地仓库
2) 远程仓库,其中在远程仓库中又分成了3种:中央仓库 、私服、其它公共库。
Maven用户直接连接远程仓库下载构件的做法是Maven不建议使用的(尤其是对一个开发团队来说),Maven的最佳实践就是使用Maven私服来构建整个团队的项目部署和管理。私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的Maven用户使用。当Maven需要下载构件的时候,它从私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,缓存在私服上之后,再为Maven的下载请求提供服务。
构建Maven私服使用Nexus,Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问 和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。Nexus 使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。Nexus支持WebDAV与LDAP安全身份认证。
构建你的Nexus
说完了私服的好处,你是不是已经等不及开始构建你的maven私服了,那么我们开始一起构建我们的私服。首先进入Nexus的网站http://www.sonatype.org/nexus/go/,找到你需要的包,下载(演示在CentOS上安装):如果你希望用一些历史版本的包,https://help.sonatype.com/repomanager3/download/download-archives---repository-manager-3,里面自行查找。 首先下载对应的包到服务器上:
mkdir tools #新建tools目录
cd tools # 进入tools目录
wget http://download.sonatype.com/nexus/3/nexus-3.14.0-04-unix.tar.gz # 下载对应的安装包
tar zxvf nexus-3.14.0-04-unix.tar.gz # 解压缩
mv nexus-3.14.0-04/ /usr/local/nexus
cd /usr/local/nexus/bin
安装java运行环境:
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
配置私服
修改nexus.rc,让root可以启动nexus,nexus.rc在/usr/local/nexus/bin/下:
vim nexus.rc,删除run_as_user前面的注释,后面加上root:run_as_user=root
然后按esc按键,输入:wq回车。然后启动nexus
./nexus run &
出现如下内容,表示启动成功
通过http://localhost:8081就可以访问了。手动同步索引(非必选项)
首先:前往maven中央仓库下载 indexer-cli-5.1.1.jar解压工具
其次下载:nexus-maven-repository-index.properties和nexus-maven-repository-index.gz
再次,将上面下载的3个文件放到同一个路径下,通过如下命令解压:
java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer
最后,拷贝索引
nexus3.x拷贝到/sonatype-work/nexus3/blobs/default,3.x
web的使用
首先访问对应的地址,然后输入默认用户名 admin、密码 admin123:
点击左侧的browse,可以看到各种repository的type,那么这些类型有什么区别呢:
Group:这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的 所有仓库资源,访问顺序取决于配置顺序3.x默认Releases,Snapshots,Central,可在web页面配置 在web界面点开
Hosted:私有仓库,专门用来存储我们自己生成的jar文件
3rd party:未发布到公网的第三方jar (3.x去除了)
Snapshots:本地项目的快照仓库
Releases:本地项目发布的正式版本
Proxy:公网上发布的jar 例如:spring
Central:中央仓库
Apache Snapshots:Apache专用快照仓库(3.x去除了)
配置maven的setting.xml(本地的全局配置)
在maven的setting.xml文件中配置私服配置,这种方式配置后所有本地使用该配置的maven项目的pom文件都无需配置私服下载相关配置(下文中192.179.101.1:8081需要替为你自己的)
<?xml version="1.0" encoding="UTF-8"?>
settingsxmlns="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">
servers>
server>
id>nexus-releasesid>
username>deploymentusername>
password>deployment123password>
server>
server>
id>nexus-snapshotsid>
username>deploymentusername>
password>deployment123password>
server>
servers>
mirrors>
mirror>
id>nexus-releasesid>
mirrorOf>*mirrorOf>
url>http://192.168.101.1:8081/content/groups/public/url>
mirror>
mirror>
id>nexus-snapshotsid>
mirrorOf>*mirrorOf>
url>http://192.168.101.1:8081/content/repositories/snapshots/url>
mirror>
mirrors>
profiles>
profile>
id>nexusid>
repositories>
repository>
id>nexus-releasesid>
url>http://192.168.101.1:8081/content/groups/public/url>
releases>
enabled>trueenabled>
releases>
snapshots>
enabled>trueenabled>
updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
repository>
id>nexus-snapshotsid>
url>http://192.168.101.1:8081/content/repositories/snapshots/url>
releases>
enabled>trueenabled>
releases>
snapshots>
enabled>trueenabled>
updatePolicy>alwaysupdatePolicy>
snapshots>
repository>
repositories>
pluginRepositories>
pluginRepository>
id>nexus-releasesid>
url>http://192.168.101.1:8081/content/groups/public/url>
releases>
enabled>trueenabled>
releases>
snapshots>
enabled>trueenabled>
updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
pluginRepository>
id>nexus-snapshotsid>
url>http://192.168.101.1:8081/content/repositories/snapshots/url>
releases>
enabled>trueenabled>
releases>
snapshots>
enabled>trueenabled>
updatePolicy>alwaysupdatePolicy>
snapshots>
pluginRepository>
pluginRepositories>
profile>
profiles>
activeProfiles>
activeProfile>nexusactiveProfile>
activeProfiles>
settings>
捷径:docker部署Nexus
docker确实是个好东西,快速,方便,使用docker部署Nexus那就是几分钟的事情具体如下命令:
docker pull sonatype/nexus3
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
启动完成后,方位http://localhost:8081就可以进入web页面了,其他操作和配置和上面的内容一致,因此这部分就不在这里描述了。
