nexus下载地址:https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/3/nexus-3.68.1-02-unix.tar.gz
Nexus服务安装
1、上传nexus到服务器/home/nexus目录
# 解压
tar -zxf nexus-3.68.1-02-unix.tar.gz
# 修改服务端口
vim /home/nexus/nexus-3.68.1-02/etc/nexus-default.properties
2、开放端口
# 开放端口
firewall-cmd --permanent --zone=public --add-port=8578/tcp
# 刷新配置
firewall-cmd --reload
3、创建只用于启动服务的用户
# 创建不能登录,只用于启动服务的用户
useradd -r -m -s /sbin/nologin nexus
# 设置用户文件权限,未知原因在nexus目录设置权限,但sonatype-work目录下有文件出现未赋权的问题,所以对两个目录分别赋权
sudo chown -R nexus:nexus /home/nexus/nexus-3.68.1-02
sudo chmod -R 755 /home/nexus/nexus-3.68.1-02
sudo chown -R nexus:nexus /home/nexus/sonatype-work
sudo chmod -R 755 /home/nexus/sonatype-work
4、配置启动项
# 创建systemd文件,输入以下配置
vim /etc/systemd/system/nexus.service
# 更新配置后需要刷新
systemctl daemon-reload
# 查看打印详细日志
journalctl -u nexus.service -xe
[Unit]
Description=Nexus Service
After=network.target
[Service]
User=nexus
Type=forking
TimeoutStopSec=20
Restart=always
RestartSec=5s
ExecStart=/home/nexus/nexus-3.68.1-02/bin/nexus start
ExecStop=/home/nexus/nexus-3.68.1-02/bin/nexus stop
[Install]
WantedBy=multi-user.target
5、设置开机自启
# 开机自启
systemctl enable nexus.service
# 关闭开机自启
systemctl disable nexus.service
# 查看开机自启的服务
systemctl list-unit-files --type=service --state=enabled | grep enabled
# 启动服务
systemctl start nexus.service
# 关闭服务
systemctl stop nexus.service
# 查看状态
systemctl status nexus.service
Nexus使用
1、登录时找到对应文件生成的密码即可
2、登录后修改密码
3、是否开启匿名访问
4.完成
5、创建maven私库
- 私库说明
- hosted,本地仓库(也叫宿主仓库),通常我们会部署自己的构件到这一类型的仓库或者是第三方的包(如:oracel的)。
- proxy,代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
- group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组
- maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
- maven-releases:私库发行版jar
- maven-snapshots:私库快照(调试版本)jar
- maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。
- Hosted有三种方式:Releases、Snapshot、Mixed
- Releases: 一般是已经发布的Jar包
- Snapshot: 未发布的版本
- Mixed:混合的
- 创建代理仓库
添加阿里云镜像:https://maven.aliyun.com/repository/public/
- 创建本地仓库
- 创建仓库组,将前面创建的仓库添加上去
6、创建用户权限
7、创建开发用户
8、maven使用私库
- settings.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">
<!-- 本地repository路径 -->
<localRepository>F:/Maven/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<!-- 指定私库使用的账号和密码,id为唯一标识 -->
<servers>
<server>
<id>mvn-public</id>
<username>dev</username>
<password>123456</password>
</server>
</servers>
<!-- 配置阿里maven仓库 -->
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<repositories>
<!-- 配置的私有仓库 -->
<repository>
<id>mvn-public</id>
<url>http://192.168.1.3:9999/repository/mvn-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!-- 用于发布项目依赖 -->
<repository>
<id>mvn_hosted</id>
<url>http://192.168.1.3:9999/repository/mvn_hosted/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!-- Maven 配置使用私服(下载插件) -->
<pluginRepositories>
<pluginRepository>
<id>mvn-public</id>
<url>http://192.168.1.3:9999/repository/mvn-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--激活profile-->
<!--将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用<activeProfiles>元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库-->
<!-- <activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles> -->
</settings>
-
发布依赖
- pom文件,添加如下配置
<distributionManagement> <repository> <id>mvn-public</id> <name>dev</name> <url>http://192.168.1.3:9999/repository/mvn_hosted/</url> </repository> </distributionManagement>
- 执行 mvn deploy将项目发布到私库
附:批量导入jar包到maven私库
1、批量导入脚本
#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
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 './mvnimport\.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}/{} ;
将文件放置到repository目录,windows使用git bash运行,linux直接运行
./mavenimport.sh -u admin -p admin123 -r http://192.168.1.3:9999/repository/mvn_hosted/