Skip to content

Nexus-Maven仓库

Nexus是Maven仓库管理器,也可以叫Maven的私服,是私服的一种。

Nexus的官网 :http://www.sonatype.com/

安装Nexus

安装JDK

bash
wget https://download.java.net/java/GA/jdk22.0.1/c7ec1332f7bb44aeba2eb341ae18aca4/8/GPL/openjdk-22.0.1_linux-x64_bin.tar.gz
tar xvf openjdk-22.0.1_linux-x64_bin.tar.gz -C /usr/local/
# 编辑环境变量配置文件
cat >> /etc/profile << "EOF"
export JAVA_HOME=/usr/local/jdk-22.0.1
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}
EOF

source /etc/profile
# 查看java版本
java -version

# 创建软连接
ln -s /usr/local/jdk-22.0.1/bin/* /usr/bin

安装Nexus

下载地址:https://www.sonatype.com/products/sonatype-nexus-oss-download

bash
tar -zxvf nexus-3.13.0-01-unix.tar.gz -C /usr/local

# 配置环境变量,添加NEXUS_HOME
cat >> /etc/profile << 'EOF'
export NEXUS_HOME=/usr/local/nexus-3.13.0-01
export PATH=$PATH:$NEXUS_HOME/bin
EOF
source /etc/profile

# 访问Web
http://ip:8081/

系统优化

bash
# 创建程序用户
useradd nexus -s /bin/bash
echo 'run_as_user="nexus"' > /usr/local/nexus-3.13.0-01/bin/nexus.rc
mkdir /usr/local/sonatype-work
chown nexus:nexus /usr/local/sonatype-work

# 设置句柄数
vim /etc/security/limits.conf
nexus - nofile 65536

创建service管理文件

bash
cat > /usr/lib/systemd/system/nexus.service << 'EOF'
[Unit]
Description=nexus service
After=network.target
 
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/usr/local/nexus-3.13.0-01/bin/nexus start
ExecStop=/usr/local/nexus-3.13.0-01/bin/nexus stop
User=nexus
Restart=on-abort
 
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable nexus --now

管理命令

bash
# 启动
nexus start
# 状态
nexus status
# 重启
nexus restart
# 强制重新刷新仓库
nexus force-reload
# 关闭
nexus stop

配置Nexus

登录Nexus Web UI,管理员默认账户密码为admin/admin123

nexus 3.13 自带的部分仓库的说明:

  • maven-central:代理仓库,该仓库代理Maven中央仓库,策略为release,因此只会下载和缓存中央仓库中的发布版本的构件。
  • maven-releases: 宿主仓库,策略为release,用来部署组织内部的发布版本的构件。
  • maven-snapshots:宿主仓库,策略为snapshots,用来部署组织内部的快照版本的构件。
  • maven-public:仓库组,包含了以上3个仓库

设置阿里云加速

最上边有个齿轮的图标,然后在菜单栏中点击Repository—>Repositorys —>找到 maven-central—>找到Proxy

  • Remote storage:填写代理加速地址https://maven.aliyun.com/nexus/content/groups/public/

然后返回Repositorys点击maven-releases找到URL这是私有Nexus地址

配置Maven

进入到Maven安装目录然后打开settings.xml

bash
# 在servers模块下添加认证信息
<server>
  <id>my-nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>
<server>
  <id>my-nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>

# 在mirror模块告诉私服地址
<mirror>
  <id>nexus</id>
  <mirrorOf>*</mirrorOf>
  <url>http://192.168.148.114:8081/repository/maven-public/</url>
</mirror>

# 在profiles模块下添加profile配置
<profile>
  <id>nexus</id>
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://192.168.148.114:8081/repository/maven-public/</url>
      <releases><enabled>true</enabled></releases>
      <snapshots> <enabled>true</enabled></snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <url>http://192.168.148.114:8081/repository/maven-public/</url>
      <releases><enabled>true</enabled></releases>
      <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
  </pluginRepositories>
</profile>

# 激活仓库
<activeProfiles>
  <activeProfile>nexus</activeProfile>
</activeProfiles>