maven私服使用场景
在实际开发中,项目中可能会用到第三方的jar、或者公司内部人员写好的公共jar包
1. 缓存公司内部使用的jar包
linux系统:
基于docker安装maven私服
步骤:
1.下载一个nexus3的镜像:
docker pull sonatype/nexus3
2.将容器内部/var/nexus-data 挂载到主机:
docker run -d -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3
记得关闭防火墙, service iptables stop 关闭防火墙
访问http://ip:8081
Maven默认账号密码
进入到容器中 :
docker run -d -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3
cat /nexus-data/admin.password
复制该密码 账号为admin
修改最新的密码为你自己的密码
访问地址:http://ip:8081/
登录
Windows系统
1.首先下载一个Nexus:官网
官方说明文档:https://help.sonatype.com/repomanager3/installation/run-as-a-service#RunasaService-Windows
Nexus 简介:
Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问 和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据
如下图:
选择windows版本
2.安装Nexus
a.解压Nexus
b.进入nexus目录:
c.从cmd中进入到当前目录输执行命令 nexus.exe /install nexus:
执行命令-启动:nexus.exe /start
访问地址:http://ip:8081/
默认用户名/密码: admin admin123
使用:
创建一个SpringBoot项目发布
创建仓库,点击Create repository,然后选择maven2(hosted)然后输入仓库名称(test-release)。在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要).
创建私服账号
点击左侧菜单栏的Users菜单,然后点击Create local user.我这里创建了一个用户,账号密码比如:admin、admin123
本地maven配置
修改本地maven的settings文件
在servers下增加server:
<server>
<id>test</id>
<username>admin</username>
<password>admin123</password>
</server>
在mirros下增加mirror:
<mirror>
<id>test</id>
<mirrorOf>*</mirrorOf>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
注意: server和mirror的id要一致
SpringBoot项目核心配置
<?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.coolhy</groupId> <artifactId>coolhy-springboot</artifactId> <version>1.0-release</version> <!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE --> <!--指定仓库地址 --> <distributionManagement> <repository> <!--此名称要和.m2/settings.xml中设置的ID一致 --> <id>test</id> <url>http://ip:8081/repository/coolhy-release/</url> </repository> </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></project> |
Maven 执行命令 mvn deploy 上传jar包即可
注意版本号码一定要是为:release版本 ,否则上传jar报错
本地项目引入maven私服jar
<dependencies> <dependency> <groupId>com.coolhy</groupId> <artifactId>coolhy_springboot</artifactId> <version>0.0.1-RELEASE</version> </dependency> </dependencies> <repositories> <repository> <id>test</id> <url>http://ip:8081/repository/coolhy-release/</url> </repository> </repositories> |
至此,项目中就可以从私服中拉取jar包了