【Linux基于Nexus安装Maven私有化仓库】
1.安装maven(需要先安装java环境)
1.在服务器创建一个存放maven数据的文件夹
mkdir /mydata/maven
cd /mydata/maven
2.下载maven安装包
我比较喜欢使用3.6.3版本,如果需要修改版本,那么只需要修改地址上两处的版本号即可
wget https://dlcdn.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
如果失效了,可自行选择版本安装,如果版本不是3.6.3,那么下面命令所有涉及版本的都需要更改成对应版本
官方下载地址
3.x版本:
https://dlcdn.apache.org/maven/maven-3/
4.x版本:
https://dlcdn.apache.org/maven/maven-4/
3.解压
tar -zxvf apache-maven-3.6.3-bin.tar.gz
4.创建一个文件夹当作maven仓库
mkdir repository
5.配置文件
cd apache-maven-3.6.3/conf
#备份原来的settings文件,重新构建一份
mv settings.xml settings.xml.bak
#重新创建一个配置文件
vim settings.xml
新的配置文件内容,暂时先配置这么多,后续细讲配置文件的内容
<settinmv settings.xml settings.xml.bakgs xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- 仓库目录 -->
<localRepository>/mydata/maven/repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
</settings>
6.环境变量配置
vim /etc/profile
在配置文件最下方添加内容
#定义环境变量maven目录
MAVEN_HOME=/mydata/maven/apache-maven-3.6.3
#把maven目录追加到path里去
PATH=$MAVEN_HOME/bin:$PATH
#把变量设置为全局变量
export PATH MAVEN_HOME
保存后退出,并重新加载环境变量
source /etc/profile
7.安装成功检测
mvn -v
安装成功!
2.安装nexus
1.创建文件目录
mkdir /mydata/nexus
cd /mydata/nexus
2.下载nexus
下载地址https://help.sonatype.com/repomanager3/product-information/download
可以自行下载然后上传到服务器安装,我这里推荐使用wget
wget https://download.sonatype.com/nexus/3/nexus-3.47.1-01-unix.tar.gz
如果提示连接失败或者超时,那么自行百度一下网友下载好的安装好上传服务器即可
3.解压
tar -zxvf nexus-3.47.1-01-unix.tar.gz
解压之后会多出来两个目录
4.启动nexus
移动到文件目录的nexus的bin里面去
这里我给出全路径,你们可以自己判断子路劲在哪
cd /mydata/nexus/nexus-3.47.1-01/bin
#启动nexus
./nexus start
#停止
./nexus stop
#查看状态
./nexus status
这样就启动成功了,然后就可以在浏览器上访问默认端口8081
如果访问不同,请排查服务器防火墙或者云服务器安全组端口设置
这样就启动成功了
5.登录
查看默认密码
还记得解压之后出现的另外一个目录吗sonatype-work
/mydata/nexus/sonatype-work/nexus3
ls
查看默认密码
cat admin.password
然后回到浏览器
默认用户名是admin
登录之后会强制修改密码
这样就登录成功了
3.使用nexus
1.页面解释
也可以自行创建私有仓库
但是这里就不一一讲解了,主要讲解基础功能,其他的需要自己单独去学习了解一下
2.可视化页面上传私有的jar包
点击Upload即可上传成功
3.服务器命令上传方式
首先需要配置服务器的maven
vim /mydata/maven/apache-maven-3.6.3/conf
配置文件内容
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- 仓库目录 -->
<localRepository>/mydata/maven/repository</localRepository>
<pluginGroups>
</pluginGroups>
<servers>
<server>
<!-- 这里id需要配置的是你nexus的登录账号密码 -->
<id>nexus</id>
<username>admin</username>
<password>自己的password</password>
</server>
</servers>
<proxies>
<!-- 引用naxus倉庫組,下面三个的id需要和以上id保持相同,才能进行匹配 -->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<!-- 这里的url需要从nexus页面上进行复制,只需要复制maven-releases 和maven-public两个仓库地址即可 -->
<url>http://ip:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://ip:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</proxies>
<activeProfiles>
<!-- 这里主要是配置使用以上哪个配置,和上面的id保持一致即可,可以配置多个 -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
上传命令是
mvn deploy:deploy-file -Dfile=xx.jar -DgroupId=com.wish -DartifactId=test -Dversion=1.0.0 -Dpackaging=Jar -Durl=http://ip:8081/repository/maven-releases/ -DrepositoryId=nexus
命令解析
-Dfile=上传的jar包相对路径或者绝对路径
-DgroupId=上传到组id
-DartifactId=上传到的名
-Dversion=上传包的版本
-Dpackaging=上传包的类型,默认jar
-Durl=上传到的仓库地址
-DrepositoryId=这里是和maven配置文件里server的id保持一致,以便识别配置的账号密码
第一次推送可以需要一定的时间,因为需要下载一些依赖文件
看到Build Success就是推送成功了
可以在nexus里面查看到
4.idea里直接打包一键上传
这个步骤区别就是在本地执行了,就不是在服务器执行了,一般这个操作是不开放的,因为这样特别容易导致版本代码混乱,但是为了同学自己练习的时候方便开发,也可以尝试
1.配置本地maven
<servers>
<!-- 用于配置下面profile访问的账号密码 -->
<!-- 此id需要和profile的id相同 -->
<server>
<id>nexus</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<!-- 中心仓库的 mirror(镜像) -->
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<!-- aliyun仓库地址 以后所有要指向中心仓库的请求,都会指向aliyun仓库-->
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<!-- 引用naxus倉庫組-->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://ip:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://ip:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>JDK-1.8</id>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>JDK-1.8</activeProfile>
<activeProfile>nexus</activeProfile>
</activeProfiles>
这样先配置了maven连接私库的配置
然后在需要打包的项目的pom.xml里配置
<distributionManagement>
<repository>
<!-- 这里的id和name需要和maven的settings里的server里保持一致-->
<id>nexus</id>
<name>admin</name>
<url>http://ip:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
然后点击
这样就可以了,然后去可视化页面看一下,是否推送成功
4.本地引用私库的jar包
1.只需要在本地maven配置引用地址即可
,这里我把我的配置文件贴出来,除了配置了私库地址之外,还配置里阿里云的maven库地址,这样除了自己私有的jar,其他的就可以在阿里云去下载了
<?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:\software\repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!-- 用于配置下面profile访问的账号密码 -->
<!-- 此id需要和profile的id相同 -->
<server>
<id>nexus</id>
<username>admin</username>
<password>PASSWORD</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<!-- 中心仓库的 mirror(镜像) -->
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<!-- aliyun仓库地址 以后所有要指向中心仓库的请求,都会指向aliyun仓库-->
<url>http://IP:8081/repository/maven-public/</url>
</mirror>
<mirror>
<id>aliyun</id>
<!-- 中心仓库的 mirror(镜像) -->
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<!-- aliyun仓库地址 以后所有要指向中心仓库的请求,都会指向aliyun仓库-->
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<!-- 引用naxus倉庫組-->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://IP:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://IP:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>alimaven</id>
<repositories>
<repository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>JDK-1.8</id>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>JDK-1.8</activeProfile>
<activeProfile>nexus</activeProfile>
<activeProfile>alimaven</activeProfile>
</activeProfiles>
</settings>
这样就可以直接在项目的pom.xml里直接引用啦,OK!
ps:文章内容性质属于笔记,由本人查看网上资料或实践后的个人理解,不保证准确性,仅供参考,如有意见,欢迎讨论指正。