文章目录
1. Maven私服介绍
私服是远程仓库的一种,假设在局域网中,一般供一个公司使用。
在pom.xml中引入某个坐标后,如果本地仓库不存在该jar包,就会先去maven私服下载;如果私服中也不存在,则私服会去中央仓库/其他公共仓库下载。
2. nexus的下载与搭建
① 下载压缩包,解压到/usr/local
目录下,并重命名为 nexus (删掉版本号)
② 后台打开
sudo /usr/local/nexus/nexus/bin/nexus start
③ 前台打开(推荐)
sudo /usr/local/nexus/nexus/bin/nexus console
④ 访问
http://localhost:8081
⑤ 输入账户密码(账户为admin,密码在下面的路径里找到)
/usr/local/nexus/sonatype-work/nexus3/admin.password
⑥ 成功!
3. nexus中的仓库类型
maven_releases(host):发行版,经过测试的成熟版本
maven_snapshots(host):快照版,仍处于开发中
maven_central(proxy):从中央仓库下载的jar包被放在这里
maven_public(group):这是一个仓库组,包含上面的三个仓库
4. 发布jar包到maven私服
既然maven私服是搭建在公司局域网上的,那么公司内的所有员工都可以将自己的项目/组件打成jar包发布到私服,这样其他人就能从私服下载这个jar包到他的本地仓库。
发布步骤三步骤:
① 配置maven的setting.xml(/usr/local/maven/conf/setting.xml
)
<server>
<id>releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
② 配置项目的pom.xml文件
<!-- 私服上传路径 -->
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
③ 执行命令
mvn deploy
另外说明一下, releases仓库中一般是经过测试的可发布版本,snapshots仓库中一般是不成熟的快照版本;具体会被上传到哪个仓库,依据的是pom.xml中的版本号(version)。
5. 从maven私服下载jar包
在maven的setting.xml(/usr/local/maven/conf/setting.xml
)中配置,一步到位:
<profiles>
<!-- 私服下载路径 -->
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<!--仓库地址(一般用的是public仓库组,因为这样可以从三个仓库中下载!!!)-->
<url>http://localhost:8081/repository/maven-public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,从私服下载插件 -->
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活上面的下载配置 -->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
完成以上配置后,如果在本地仓库和私服中都找不到某个jar包,那么就会由私服去中央仓库下载,然后本地仓库再从私服下载该jar包(该jar包被放在了私服的public仓库组的central仓库中);
不过如果你已经在maven的setting.xml中配置了阿里云镜像,那么私服中不会有jar包,而是从阿里云直接下载到本地仓库。
6. 将第三方jar包安装到maven本地仓库
并不是所有jar包中央仓库都有,有的第三方jar我们只能从它的官网下载,然后再安装到maven本地仓库/maven私服中,所有才有了这两节的内容。
终端中输入命令(比较长但不难理解 >_<):
mvn install:install-file -Dfile=xxx/xxx/xxxxxx.jar -DgroupId=xxxxxx -DartifactId=xxxxxx –Dversion=xxxxxx -Dpackaging=jar
7. 将第三方jar包安装到maven私服
① 我们习惯将第三方jar包放在3rd_party仓库中。但在nexus3中已经删除了该默认仓库,因此需要我们手动创建仓库,并将它加入到public仓库组中(这篇博客有清晰的演示)
② 在maven的setting.xml(/usr/local/maven/conf/setting.xml
)中配置一下该仓库的账户密码:
<server>
<id>3rd_party</id>
<username>admin</username>
<password>123456</password>
</server>
③ 终端中输入命令(比较长但不难理解 >_<):
mvn deploy:deploy-file -Dfile=xxx/xxx/xxxxxx.jar -DgroupId=xxxxxx -DartifactId=xxxxxx Dversion=xxxxxx -Dpackaging=jar –Durl=http://localhost:8081/nexus/xxxxxx/ -DrepositoryId=xxxxxxx