目的:运用docker和Nexus来搭建私服Maven仓库
过程:
1、安装docker,然后通过docker来下载Nexus image,运行docker命令:
docker pull sonatype/nexus3
有些由于权限问题需要加sudo来运行。
2、因为我要和本地文件夹挂载,所以先建立本地文件夹,并将要挂载的文件夹的owner改为200,如下:
mkdir /angusyang/nexus/nexus-data
chown -R 200 /angusyang/nexus/nexus-data
3、用docker运行nexus服务,如下:
docker run -d -p 6130:8081 --name nexus -v /angusyang/nexus/nexus-data:/nexus-data sonatype/nexus3
这里简单说明,-p 后面是端口映射,冒号前面的是本地机器的端口,后面的是nexus抛出的端口;-v 是挂载,冒号前面是本地路径,后面是nexus的共享路径。
4、运行正常后http访问服务器及端口应该能看到Nexus的界面,并对Nexus进行相关配置,例如:http://localhost:6130
登陆进去后配置相关信息,默认的管理员账号:admin,密码:admin123
在上图可以新增配置仓库来源等等,另外可以在maven-public中加入你想要访问的仓库,有优先顺序,前面的找不到才会去后面的仓库继续找,例如:
其他权限等等配置就不在此说明了,另外简单说明一下,我建立了一个maven-3rd-central仓库,改仓库是存放maven仓库找不到的一些第三方的jar,简单列出推送jar到该仓库的命令(该命令要配置maven的settings.xml文件后执行):
mvn deploy:deploy-file
-DgroupId=com.tibco
-DartifactId=jms
-Dversion=2.0
-Dpackaging=jar
-Dfile=D:jms-2.0.jar
-Durl=http://localhost:6130/repository/maven-3rd-central/
-DrepositoryId=nexus
5、配置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"> <localRepository>D:/apache_maven_3_5_0/repository</localRepository> <servers> <server> <id>nexus</id> <username>admin</username> <password>admin123</password> </server> </servers> <mirrors> <mirror> <!--This sends everything else to /public --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://eellnx34.eel1.esquel.com:6130/repository/maven-public/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <!--Enable snapshots for the built in central repo to direct --> <!--all requests to nexus via the mirror --> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>
6、我们在开发时,也需要对pom.xml文件进行配置,以便将打包的jar上传到私服中去,例如:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.angusyang</groupId> <artifactId>test-nexus</artifactId> <version>1.0-SNAPSHOT</version> <distributionManagement> <repository> <id>nexus</id> <name>Releases</name> <url>http://localhost:6130/repository/maven-releases</url> </repository> <snapshotRepository> <id>nexus</id> <name>Snapshot</name> <url>http://localhost:6130/repository/maven-snapshots</url> </snapshotRepository> </distributionManagement> ...... </project>
这样基本就配置完成了,其他的就是Nexus的一些配置了,这里就不讲述了!