docker安装
1、下载镜像
docker pull sonatype/nexus3
2、将容器内部/var/nexus-data挂载到主机/data/docker/nexus/data目录
docker run -d -p 4070:8081 --name nexus -v /data/docker/nexus/data:/var/nexus-data sonatype/nexus3
3、开放端口
firewall-cmd --zone=public --add-port=4070/tcp --permanent
4、访问http://192.168.56.100:4070 ,Maven私服启动容器稍微比较慢,等待1分钟即可,默认登陆账号 admin,密码如果admin123不行,则查看 /data/docker/nexus/data/admin.password
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES50fb70de8f66 sonatype/nexus3 "sh -c ${SONATYPE_DI…" 3 minutes ago Up 3 minutes 0.0.0.0:4070->8081/tcp nexus
5、查看日志:
docker logs -f nexus
docker-compose安装
docker-compose.yml
version: '3'services:nexus:restart: alwaysimage: sonatype/nexus3container_name: nexusports:- 4070:8081volumes:- /data/docker/nexus/data:/nexus-data
出现异常:
java.io.FileNotFoundException: ../sonatype-work/nexus3/tmp/i4j_ZTDnGON8hezynsMX2ZCYAVDtQog=.lock (No such file or directory)
目录权限问题,修改为757后正常启动:
chmod 757 /data/docker/nexus/data/
另外一种推荐做法是,使用容器挂载数据比较不容易出现文件权限的问题。
docker volume create --name nexus-data
然后修改docker-compose.yml:
version: '3.1'services:nexus:image: sonatype/nexus3container_name: nexusrestart: alwaysports:- 4070:8081volumes:- data:/nexus-datavolumes:data:
使用
1、创建私服仓库
创建仓库,点击Create repository,然后选择maven2(hosted)然后输入仓库名称 nexus-release。在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要).
2、创建私服账号
点击左侧菜单栏的Users菜单,然后点击Create local user,我这里创建了一个用户,账号密码都是:test
本地settings.xml
<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>
Snapshots 与 Releases 的区别
- nexus-releases: 用于发布 Release 版本
- nexus-snapshots: 用于发布 Snapshot 版本(快照版)
Release 版本与 Snapshot 定义如下:
Release: 1.0.0/1.0.0-RELEASESnapshot: 1.0.0-SNAPSHOT
- 在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。
- SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar
3、创建一个Maven工程
创建一个maven工程,并且打包到maven私服。
相关配置
<distributionManagement><repository><id>nexus-releases</id><name>Nexus Release Repository</name><url>http://192.168.56.100:4070/repository/nexus-releases/</url></repository><snapshotRepository><id>nexus-snapshots</id><name>Nexus Snapshot Repository</name><url>http://192.168.56.100:4070/repository/nexus-snapshots/</url></snapshotRepository></distributionManagement><build><plugins><!--发布代码Jar插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><version>2.7</version></plugin><!--发布源码插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>2.2.1</version><executions><execution><phase>package</phase><goals><goal>jar</goal></goals></execution></executions></plugin></plugins></build>
注意事项:
- ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致。
- 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。
4、部署jar包:
mvn deploy
5、上传第三方 JAR 包
Nexus 3.0 不支持页面上传,可使用 maven 命令:
# 如第三方JAR包:aliyun-sdk-oss-2.2.3.jarmvn deploy:deploy-file-DgroupId=com.aliyun.oss-DartifactId=aliyun-sdk-oss-Dversion=2.2.3-Dpackaging=jar-Dfile=~/aliyun-sdk-oss-2.2.3.jar-Durl=http://192.168.56.100:8081/repository/nexus-releases/-DrepositoryId=nexus-releases
注意事项:
- 建议在上传第三方 JAR 包时,创建单独的第三方 JAR 包管理仓库,便于管理有维护。(maven-3rd)
- -DrepositoryId=nexus-releases 对应的是 settings.xml 中 Servers 配置的 ID 名称。(授权)
5、配置代理仓库
<repositories><repository><id>nexus-releases</id><name>Nexus Repository</name><url>http://192.168.56.100:4070/repository/nexus-releases/</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.56.100:4070/repository/nexus-releases/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>true</enabled></releases></pluginRepository></pluginRepositories>
