拉取Nexus镜像
docker pull sonatype/nexus3
docker-compose.yml
version: '3.1'
services:
nexus:
restart: always
image: sonatype/nexus3
container_name: nexus
ports:
- 8081:8081
volumes:
- /usr/local/docker/nexus/data:/nexus-data
注意:数据卷一定要给读写执行的权限 Nexus不能启动可能正是因为此原因
搭建好了私服之后如何让我们的代码关联Nexus?
在 Maven settings.xml
中添加 Nexus 认证信息(servers
节点下):
<!--发行版服务节点-->
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!--快照办服务节点-->
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
配置自动化部署
配置在pom.xml文件中
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.103.131:8081/repository/maven-releases/</url> #发行版仓库地址
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.103.131:8081/repository/maven-snapshots/</url> #快照版仓库地址
</snapshotRepository>
</distributionManagement>
注意事项:
- ID 名称必须要与
settings.xml
中 Servers 配置的 ID 名称保持一致。 - 项目版本号中有
SNAPSHOT
标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。
部署到仓库
mvn deploy #将项目打包放在maven中
将jar包上传至Nenux私服
#此处以上传gson.jar为例
mvn deploy:deploy-file
-DgroupId=com.google.co #此处与groupId对应
-DartifactId=gson #此处与artifactId对应
-Dversion=2.8.5 #此处与version对应
-Dpackaging=jar #上传文件格式为jar
-Dfile=D:\jar\gson-2.8.5.jar #选择你jar包的位置
-Durl=http://192.168.106.131:8081/repository/maven-releases/ #上传至你maven私服的位置
-DrepositoryId=nexus-releases #此处Id一定要和你Pom.xml文件中配置的ID一致
#以下配置信息作为参考
--------------------------------------分界线------------------------------------------
----------------------------------------此处节点配置在pom.xml文件中--------------
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
------------------这个是配置你私服的位置便于maven找到你的maven私服,别忘了在maven仓库的
setting.xml中配置私服的账户和密码
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.106.131:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.106.131:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
如何让项目在确实jar包的情况下先找到私服下载再找官服
配置在pom.xml文件中
<!--配置此节点信息,若项目所需项目在本地仓库找不到则去这里配置的私服下载,不会直接去官服下载-->
<!--从私服下载-->
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://192.168.106.131:8081/repository/maven-public/</url>
<snapshots> <!--是否可以依赖快照版-->
<enabled>true</enabled>
</snapshots>
<releases> <!--是否可以依赖发行版-->
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus Plugin Repository</name>
<url>http://192.168.106.131:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>