maven私服-nexus
下载安装
下载nexus
访问官网: https://help.sonatype.com/repomanager3/product-information/download
在服务器中创建文件夹:nexus
上传压缩包到文件夹中
解压文件
tar -zxvf nexus-3.61.0-02-unix.tar.gz
nexus 配置
进入nexus-3.61.0-02/bin 文件夹
/usr/local/software/nexus/nexus-3.61.0-02/bin
编辑nexus.vmoptions文件
根据自己机器内存大小,适当配置内存。内存太小未来启动nexus会失败。
vim nexus.vmoptions
这个是已经修改过的
配置端口
- 进入etc文件夹
- 查看nexus-default.properties文件
这里我没有修改,如果修改了以后 :wq 保存退出,然后开放防火墙端口
[root@localhost etc]# firewall-cmd --add-port=8081/tcp --permanent
success
[root@localhost etc]# firewall-cmd --reload
success
运行nexus
启动nexus
./nexus start //启动命令
查看nexus的运行状态
在浏览器中输入 http://192.168.107.130:8081 //IP是自己的服务器IP
修改管理员密码
- 查看管理员密码
- 进入sonatype-work\nexus3文件夹
2.查看管理员密码
- 修改管理员密码
1.浏览器登录nexus
2.修改密码
一定要修改密码,默认的密码是一次性的
配置私有仓库
nexus中默认仓库
maven-releases (Version policy=Release)默认只允许上传不带SNAPSHOT版本尾缀的包,默认部署策略是Disable redeploy 不允许重复上传相同版本号信息的jar,避免包版本更新以后使用方无法获取到最新的包。
maven-snapshots (Version policy=Snapshot)只允许上传带SNAPSHOT版本尾缀的包,默认部署策略是Allow redeploy,允许重复上传相同版本号信息的jar,每次上传的时候会在jar的版本号上面增加时间后缀信息。
maven-central 中央仓库的拷贝,如果环境可以访问中央仓库,则可以获取到相关的包,否则没用
maven-public 仓库组,不是实际个一个仓库地址,只是将现有的组合到一次,可以通过它看到所属组内全部仓库的jar信息
创建自定义仓库
添加repository仓库
选择maven2(hosted)
添加release仓库
添加snapshot仓库
添加新建仓库到maven-public群组中
选中maven-public群组,之后访问maven-public就可以访问自己的私有仓库了
另外2个同理
批量上传本地文件到自定义仓库中
上传本地仓库内容到linux服务器
记得提前创建文件夹,上传到文件夹中
找到自己的本地仓库位置
全选然后点击上传
等待上传完成
编辑批量上传脚本
在本地仓库上传的文件夹(maven-repository)下创建一个shell脚本,命名 repo.sh
- 创建脚本
touch repo.sh
- 编辑脚本
vim repo.sh
把下面的代码复制进去
#!/bin/bash
while getopts ":r:u:p:" opt; do
case $opt in
r) REPO_URL="$OPTARG"
;;
u) USERNAME="$OPTARG"
;;
p) PASSWORD="$OPTARG"
;;
esac
done
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
添加权限
给脚本repo.sh添加执行权限
chmod +x repo.sh
添加之前
变成
导入本地仓库到nexus私有仓库
执行以下命令
./repo.sh -u nexus用户名 -p nexus密码 -r 仓库地址
./repo.sh -u admin -p 123 -r http://192.168.107.130:8081/repository/wnhz-repository/
等待上传完成
上传完成以后这里就可以看到
项目中引用nexus库
在maven的conf/settings.xml中配置server
id: 名字不能重复,和pom引入名称相同
username/password: nexus的登录用户名密码
<server>
<id>wnhz</id>
<username>admin</username>
<password>123</password>
</server>
<server>
<id>wnhz-releases</id>
<username>admin</username>
<password>123</password>
</server>
<server>
<id>wnhz-snapshots</id>
<username>admin</username>
<password>123</password>
</server>
在maven中配置镜像
中央仓库的资源从阿里云访问,其它资源来自nexus私服
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus djs</name>
<url>http://192.168.0.101:8081/repository/maven-public/</url>
</mirror>
</mirrors>
私服访问地址从下图获取:http://192.168.107.130:8081/repository/maven-public/
nexus仓库配置
<profile>
<!--profile 的 id-->
<id>nexus</id>
<repositories>
<repository>
<!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
<id>nexus</id>
<!--仓库地址,即 nexus 仓库组的地址-->
<url>http://192.168.201.81:8081/repository/maven-public/</url>
<!--是否下载 releases 构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载 snapshots 构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.201.81:8081/repository/maven-public/</url>
<!--是否下载 releases 构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载 snapshots 构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
activeprofile
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
项目中配置发布管理
在项目的pom.xml文件中添加
<distributionManagement>
<distributionManagement>
<snapshotRepository>
<id>wnhz-snapshots</id>
<url>http://192.168.201.81:8081/repository/wnhz-snapshots/</url>
</snapshotRepository>
<repository>
<id>wnhz-releases</id>
<url>http://192.168.201.81:8081/repository/wnhz-releases/</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.201.81/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
部署项目到私服
在maven中运行部署,部署(deploy)项目到私服。