安装Nexus私服
1.安装JDK和Nexus
Nexus下载地址:
https://www.sonatype.com/download-oss-sonatype
安装命令:
查看linux 服务器默认java并卸载
rpm -qa | grep java
rpm -qa | grep jre
rpm -qa | grep jdk
rpm -e --nodeps java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64 java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64 java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
安装下载好的jdk并设置环境变量
rpm -ivh jdk-8u131-linux-x64.rpm
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_131/
export PATH=$PATH:$JAVA_HOME
source /etc/profile
tar -zxf nexus-3.25.0-03-unix.tar.gz
mv nexus-3.25.0-03/ nexus
2.systemd启动服务
cat >/usr/lib/systemd/system/nexus.service<<EOF
[Unit]
Description=nexus
[Service]
ExecStart=/data/nexus/bin/nexus start
ExecStop=/data/nexus/bin/nexus stop
Type=forking
User=root
Group=root
[Install]
WantedBy=multi-user.target
EOF
默认不能用root用户启动修改,修改User Group
3.修改jvm内存使用大小
默认2G内存,我们修改为1G,根据服务器内存自行调整
vim /opt/nexus/bin/nexus.vmoptions
-Xms1024m
-Xmx1024m
-XX:MaxDirectMemorySize=1024m
4.启动并检查
systemctl daemon-reload
systemctl start nexus
netstat -anpult|grep 8081
systemctl status nexus
5.配置账户密码
打开浏览器并登陆
http://服务器IP:8081/
默认账号为admin,初始密码保存在文件里:
cat /opt/sonatype-work/nexus3/admin.password
6.初始化操作
登录后需要我们修改密码:
配置Nexus仓库代理地址为国内源
默认仓库说明
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
仓库类型
Group:这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的,用于方便开发人员自己设定的仓库。maven-public就是一个Group类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认Releases,Snapshots,Central,当然你也可以自己设置。
Hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
3rd party:未发布到公网的第三方jar (3.x去除了)
Snapshots:本地项目的快照仓库
Releases: 本地项目发布的正式版本
Proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
Central:中央仓库
Apache Snapshots:Apache专用快照仓库(3.x去除了)
修改maven仓库地址
将代理地址修改为阿里源:
https://maven.aliyun.com/nexus/content/groups/public
nexus结构
根目录
- nexus安装目录
- sonatype-work\nexus3 数据文档
nexus安装目录
bin 包含nexus的启动脚本和相关配置
etc jetty、karaf等配置文件
jre jre环境
lib java架包库
public 关于nexus应用在本地跑起来所需要的资源
system 应用所有的插件和组件
sonatype-work\nexus3 目录
blobs/ 创建blob的默认路径,当然也可以重新指定
cache/ 当前缓存的karaf包的信息
db/ OrientDB数据库的数据,用于存储nexus的元数据的数据库
elasticsearch/ 当前配置的Elasticsearch状态
etc/ 大概是运行时配置状态和关于资源库的自定义的相关的东西
health-check/ 看目录,健康检查的相关报告的存储目录吧
keystores/ 自动生成的关于资源库的ID主键
log/ 运行实例生成的日志文件,也有日志文件的压缩包,貌似是每天都会生成日志文件,你可以定期删除老的日志文件
tmp/ 用于存储临时文件的目录
创建 Blob Strores
nexus3 使用了Blob Stores来保存仓库文件。因此创建仓库前,可以先创建 Blob Stores。当然你也可以不创建,那么将会使用默认的路径:./sonatype-work/nexus3/blobs
创建 Proxy Repository 代理仓库
配置 Proxy Repository代理仓库。创建页面出来之后,页面上有三个框是必填的:
注意:可以多建几个proxy repository,以便于可以加入更多的远程maven仓库到我们的 group 仓库中。唯一一点,就是看加入的proxy资源库使用的是国内的还是国外的远程仓库。加入group资源库的顺序应是:hosted仓库 > 国内proxy仓库 > 国外proxy仓库。
创建 hosted repository 宿主仓库
Hosted有三种方式:Releases、Snapshot、Mixed
Releases: 一般是已经发布的Jar包
Snapshot: 未发布的版本
Mixed:混合的
创建 group repository 仓库组
将hosted repositories【宿主仓库】的顺序放在proxy repositories【代理仓库】之前,因为一个group【仓库组】中可以涵括这些宿主仓库和代理仓库。而一整个的group是作为一个public,一个接口给别人使用的。
所以当查找架包的时候,如果代理资源库在前面,那就是先从远程去查找jar,而不是先从宿主仓库(本地仓库)去查找是否有jar
让 Maven 项目使用 Nexus 私服
- Maven 下的setting.xml文件和你自己项目中的pom.xml文件的关系:
- settting.xml 文件就是本地电脑上的全局变量;而pom.xml文件就是局部变量。
pom.xml文件对于项目来说,是优先使用的。然而pom.xml文件中如果没有指定jar要从哪里下载的话,也就是说没有配镜像地址的话,当然只能去按照settting.xml 中定义的地址去找了
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="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">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>snapshots</id>
<username>admin</username>
<password>密码</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>密码</password>
</server>
<server>
<id>public</id>
<username>admin</username>
<password>密码</password>
</server>
</servers>
<mirrors>
<mirror>
<id>public</id>
<mirrorOf>*</mirrorOf>
<url>http://服务器IP:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>public</id>
<repositories>
<repository>
<id>public</id>
<url>http://服务器IP:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<url>http://服务器IP:8081/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>public</activeProfile>
</activeProfiles>
</settings>
部署上传Jar:配置distributionManagement
配置 distributionManagement
节点,执行 deploy
命令时,会将jar发布到指定地址。
- pom.xml:
<project>
...
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
...
</project>
- settings.xml:
<settings>
...
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
...
</settings>
两个文件的 id
需要保持一致