Maven仓库搭建和使用(求点赞~)
1.准备环境和注意事项
服务器:至少还有2.5G内存空间&&Docker环境
服务器内存查看指令: free -h
注意:如果在内存不够的情况下进行nexus的搭建,会导致服务器内存溢出,重启服务器可解决,重启后可能出现docker指令无法使用的情况,重启docker服务 systemctl restart docker 即可
2.安装步骤
2.1查找nexus镜像
docker search nexus
2.2 拉取镜像
docker pull docker.io/sonatype/nexus3
2.3创建容器挂载到服务器的文件夹并赋权限
mkdir -p /usr/local/nexus3/nexus-data
chown -R 200 /usr/local/nexus3/nexus-data
2.4创建容器并运行
docker run -tid -p 8081:8081 --name nexus -e NEXUS_CONTEXT=nexus -v /usr/local/nexus3/nexus-data:/nexus-data docker.io/sonatype/nexus3
3.查看网址
3.1查看默认密码
http://ip:8081/nexus/
账号:admin
首次安装密码需进入容器查看自己容器id
查看容器: docker ps -a
进入容器: docker exec -it 容器id /bin/bash
找到密码: cd /nexus-data 在文件夹下 cat admin.password (修改密码后,此文件会消失),这里的字符串就是密码,不是加密的
3.2配置代理
登录成功后会提示修改密码,修改密码即可
进入管理界面:
Browse可以查看当前有多少仓库,搭建好的Nexus,默认会带有一些maven仓库,一般使用这些仓库就足够了。
默认仓库说明
默认仓库 作用
maven-central maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases 私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
maven-snapshots 私库快照(调试版本)jar
maven-public 仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml或项目pom.xml中使用
仓库类型 作用
Group 这是一个仓库聚合的概念,用户仓库地址选择Group的地址,即可访问Group中配置的,用于方便开发人员自己设定的仓库。maven-public就是一个Group类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x默认Releases,Snapshots,Central,当然你也可以自己设置。
Hosted 私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的jar文件
3rd party 未发布到公网的第三方jar (3.x去除了)
Snapshots 本地项目的快照仓库
Releases 本地项目发布的正式版本
Proxy 代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
Central 中央仓库
Apache Snapshots Apache专用快照仓库(3.x去除了)
3.3增加新的代理源:
添加代理Cache统一设置为200天 288000:
逐步增加常见代理
代理名 Url
aliyun http://maven.aliyun.com/nexus/content/groups/public
apache_snapshot https://repository.apache.org/content/repositories/snapshots/
apache_release https://repository.apache.org/content/repositories/releases/
atlassian https://maven.atlassian.com/content/repositories/atlassian-public/
central.maven.org http://central.maven.org/maven2/
datanucleus http://www.datanucleus.org/downloads/maven2
maven-central (安装后自带,仅需设置Cache有效期即可) https://repo1.maven.org/maven2/
nexus.axiomalaska.com http://nexus.axiomalaska.com/nexus/content/repositories/public
oss.sonatype.org https://oss.sonatype.org/content/repositories/snapshots
pentaho https://public.nexus.pentaho.org/content/groups/omni/
3.4设置maven-public 将这些代理加入Group,最好将默认的maven库放到最底下
3.5 设置私用仓库可重复发布
4.Maven配置使用Nexus
<?xml version="1.0" encoding="UTF-8"?>${user.home}/.m2/repository
<server>
<id>releases</id>
<username>admin</username>
<password>admin23</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
HolliParkMirror * HolliPark Repository Mirror. http://localhost:8081/nexus/repository/maven-public/
HolliPark nexus Public Repositories http://localhost:8081/nexus/repository/maven-public/ true
<repository>
<id>central</id>
<name>Central Repositories</name>
<url>http://localhost:8081/nexus/repository/maven-central/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>release</id>
<name>Release Repositories</name>
<url>http://localhost:8081/nexus/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<name>Snapshot Repositories</name>
<url>http://localhost:8081/nexus/repository/maven-snapshots/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>plugins</id>
<name>Plugin Repositories</name>
<url>http://localhost:8081/nexus/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
HolliPark ## 5.上传项目到maven仓库 在pom文件中加入以下依赖: releases Nexus Release Repository http://localhost:8081/nexus/repository/maven-releases/ snapshots Nexus Snapshot Repository http://localhost:8081/nexus/repository/maven-snapshots/ **执行:mvn deploy** 查看 ![](https://img-blog.csdnimg.cn/img_convert/61d769093518a8068604e96c72f65652.png) **注意以下几点**: 若项目版本号末尾带有 -SNAPSHOT,则会发布到snapshots快照版本仓库 若项目版本号末尾带有 -RELEASES 或什么都不带,则会发布到releases正式版本仓库 ## 6.Jar 使用,引入pom即可 # 第一次写,不太会吖,求评论