文章目录
一、安装docker
二、docker-compose.xml
version: "3.6"
services:
nexus:
image: sonatype/nexus3:3.68.1-java11
container_name: nexus
volumes:
- ./data:/nexus-data
ports:
- "8081:8081"
restart: unless-stopped
platform: linux/amd64 #m1的电脑需要加上这个配置
environment:
- TZ=Asia/Shanghai
三、第一次登录
http://ip:8081
账号:admin
密码:在3.17.0之后的版本,密码已不是默认的admin123,可以进入容器执行以下指令查看:
cat /home/nexus/data/admin.password
四、创建nexus存储库
登录nexus后,设置》Repository》Repositories可以看到nexus默认已经创建了存储库,我这里直接使用了默认库,没必要再创建一套,有需要单独创建的及每个库的含义可以网上查一下,这里先不细说。
可以发现这里还是创建了一个ali-central的代理库,因为默认库配置的是maven官方的地址,下载包的时候会慢一点,ali-central添加了阿里镜像地址,这样本地会先从ali-central中下载jar,配置如下:
创建完成后需要在maven-public中添加ali-central,这样在settings中只需要配置maven-public即可:
值得注意的是,这里的members是有加载顺序的,请确保把你想优先访问的库放在前面。
五、本地maven配置
setting.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">
<localRepository>/you_address/.m2/repository</localRepository>
<servers>
<!-- 如果需要上传jar包到nexus,加上这里 -->
<server>
<id>maven-releases</id>
<username>admin</username>
<password>you_password</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>you_password</password>
</server>
<!-- nexus的账号密码,如果只需要去私服下载jar包则只需配置此项 -->
<server>
<id>maven-public</id>
<username>admin</username>
<password>you_password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-public</id>
<mirrorOf>central</mirrorOf>
<name>maven-public</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<!-- 对应activeProfiles-activeProfile的内容 -->
<id>nexus</id>
<!-- 仓库地址 -->
<repositories>
<repository>
<!-- 私服id,覆盖maven-model模块下的父id,让maven不走中央仓库下载,走私服下载 -->
<id>central</id>
<!-- 名字 -->
<name>Nexus</name>
<!-- 私服地址,写central后,会去mirror里面找 -->
<url>http://central</url>
<!-- 支持releases版本 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 支持snapshots版本 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件地址 -->
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Nexus Plugin Repository</name>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!-- 配置全局的url地址 供上传jar包时动态获取 -->
<properties>
<ReleaseRepository>http://ip:8081/repository/maven-releases/</ReleaseRepository>
<SnapshotRepository>http://ip:8081/repository/maven-snapshots/</SnapshotRepository>
</properties>
</profile>
</profiles>
<!-- 选择使用的profile -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
六、pom文件配置
如果本地想上传jar到nexus管理,需要在pom添加以下配置:
<!--推送地址配置,id需和settings.xml中的server的id配置一致-->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>releases</name>
<url>http://ip:8081/repository/maven-releases/</url>
<uniqueVersion>false</uniqueVersion>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>snapshots</name>
<url>http://ip:8081/repository/maven-snapshots/</url>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
</distributionManagement>
七、测试
maven build
至此,nexus基本配置已经完成,本地maven build完成后可以在nexus上看到加载到的jar
上传第三方包
通过指令
mvn deploy:deploy-file -DgroupId=antlr -DartifactId=antlr -Dversion=2.7.7 -Dpackaging=jar -DrepositoryId=maven-release -Durl=http://192.168.31.111:8090/repository/maven-releases -Dfile=antlr-2.7.7.jar
DgroupId和DartifactId构成了该jar包在pom.xml的坐标,项目就是依靠这两个属性定位。自己起名字也行。
Dfile表示需要上传的jar包的绝对路径。
Durl私服上仓库的位置,打开nexus——>repositories菜单,可以看到该路径。
DrepositoryId服务器的表示id,在nexus的configuration可以看到。
Dversion表示版本信息,得到一个jar包准确的版本
使用idea
如果有自定义的starter要经常维护,用idea打包上传是个不错的方式:
结束
今天就想到这些,后续可能还会持续添加一些使用时遇到的问题。大佬们遇到问题或者文章有不对的地方可以留言讨论~