文章目录
maven私服安装与配置
一、nexus私服的安装
1. nexus下载
nexus下载地址–>nexus2.x下载地址
2. 安装私服nexus
- 上传服务到Linux服务器并解压
# 创建文件夹
mkdir -p nexus
# 复制文件到nexus文件夹
cp -r nexus-2.14.20-02-bundle.tar.gz nexus/
# 进入文件文件夹并解压
cd nexus
tar xzvf nexus-2.14.20-02-bundle.tar.gz
解压后存在连个文件夹 一个 nexus-2.14.20-02 和 sonatype-work
- 配置环境
编辑运行的用户
# 进入到启动目录
cd /usr/local/nexus/nexus-2.14.20-02/bin
# 编辑启动的环境
vim nexus
# 将 RUN_AS_USER设置为root 如下图
RUN_AS_USER=root
编辑运行的java环境
# 进入到目录
cd /usr/local/nexus/nexus-2.14.20-02/bin/jsw/conf
# 编辑java的运行环境
vim wrapper.conf
# 将wrapper.java.command=java 设置为实际的java运行环境 保存退出
wrapper.java.command=/usr/local/jdk1.8.0_281/bin/java
启动nexus项目
# 进入到目录中
cd /usr/local/nexus/nexus-2.14.20-02/bin
# 启动nexus
./nexus start
访问端口 出现如下界面表示成功
二、项目使用私服下载jar包
如果只是从私服获取依赖那么只需要在maven的setting.xml配置文件中配置镜像地址,镜像地址设置public群组,nexus有一个群组属性将多个库聚合到一起,所以直接引用public库即可
<!--
id: 起一个标记
name: 介绍
url: 组仓库的地址
mirrorOf: 引组里面哪些仓库,“*”为全部
-->
<mirror>
<id>public</id>
<name>public repositroy</name>
<url>http://8.140.135.169:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
1、新建maven项目 maven_use
2、引入jar包 刷新maven
如图正在从私服上下载需要的公共发行的jar包
三、上传jar到私服
1、首先登录私服并创建两个仓库
1-1 使用账号:admin 密码:admin123登录到私服
1-2创建一个Hosted类型releases
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接
1-3创建一个Hosted类型的snapshots私服(nexus_snapshots)
2、maven配置
**2-1、 在maven安装配置目录下加入maven仓库的账号与验证码 **
<!--需要在setting.xml配置文件中添加私服的用户名和密码 来作为验证私服信息-->
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus_snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
3、新建maven项目 (maven_service)并打包上传到私服
3-1、新建maven项目并创建utils类
3-2、设置pom文件私服并上传
设置项目pom文件 添加私服上传的位置
<distributionManagement>
<!-- 上传稳定版或者发行版 -->
<repository>
<id>nexus</id> <!-- id对应server密码的id -->
<url>http://8.140.135.169:8081/nexus/content/repositories/nexus/</url>
</repository>
<!-- 上传开发板,或者不稳定版 -->
<snapshotRepository>
<id>nexus_snapshots</id> <!-- id对应server密码的id -->
<url>http://8.140.135.169:8081/nexus/content/repositories/nexus_snapshots/</url>
</snapshotRepository>
</distributionManagement>
在idea中打包上传
# 打包上传到私服
mvn package deploy
上传中
上传完成
由于我们发布的版本是1.0-SNAPSHOT版本 所以上传的时候会上传到nexus__snapshosts的仓库中,如果需要发布正式环境包直接发发布版本为1.0即可无需带SNAPSHOTS
四、项目引入私服上的jar包、
1、创建maven_use项目
2、引入maven_service项目
<dependency>
<groupId>com.matcha</groupId>
<artifactId>maven_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
刷新maven后报错 因为私服的包是不能直接引用的
3、两种解决方式
3-1、直接在项目中的pom中指明使用哪个maven私服
<!--方式一,配置jar的下载环境-->
<repositories>
<repository>
<id>public</id>
<name>local_nexus</name>
<url>http://8.140.135.169:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
3-2、在maven的配置文件增加私服的配置
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>public</id> <!-- 自定义 url为组的地址, 定义为public也就是public即可,因为组内包含多个仓库 -->
<url>http://8.140.135.169:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<!--设置未发行的jar直接从maven私服获取-->
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
<!--激活配置-->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
4、测试运行项目
调用maven_service项目中的AppServiceUtils类的方法,输入 说明完成;