nexus/简洁的maven私服搭建说明(maven nexus3)
Maven
现代项目几乎没有不知道maven的,这个现代项目构建工具,即使现在有了gradle,maven也保持了它在构建工具方面持久的活力。
而我们在进行业务项目开发时,往往需要开发我们公司内部的一些私有服务jar包,不希望这些私有jar包放在maven的中央仓库中,因此我们需要在公司内部搭建一个maven私服,这是maven私服的最常见的应用场景了。
传送门
nexus repository oss:https://www.sonatype.com/products/repository-oss?topnav=true
下载地址:
https://www.sonatype.com/products/repository-oss-download?hsLang=en-us,需要在下载页面稍微填写一些下载信息,然后点击下载即可。点击下载后,官网会向你的邮箱中发送一封邮件,里面有安装手册的链接。下载成功后获取到一个latest-unix.tar.gz
安装步骤
- 使用sftp上传压缩包到服务器的/root/nexus文件夹;
- 解压文件:tar -xvzf latest-unix.tar.gz
- 得到两个文件夹nexus-3.38.0-01和sonatype-work
- cd nexus-3.38.0-01/bin & ./nexus start
- 日志路径:/root/nexus/sonatype-work/nexus3/log
- 启动成功后,使用浏览器打开:http://192.168.0.13:8081
- 点击Sign in,根据提示可在服务器上查看密码信息,输入服务器上文件中的密码后根据提示修改密码即可
- 浏览器打开后,可以看Browse中最初的样子:
仓库设置
Browse可以查看当前有多少仓库,搭建好的Nexus,默认会带有一些maven仓库,一般使用这些仓库就足够了。
- 默认仓库说明
maven-central:maven中央库,默认从
https://repo1.maven.org/maven2/拉取jarmaven-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文件
Snapshots:本地项目的快照仓库
Releases: 本地项目发布的正式版本
Proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径),如可配置阿里云maven仓库
Central:中央仓库
增加新的代理源
按照步骤添加新的代理源,点击【设置-Respositories-Create repository】
页面跳转后,点击maven2(proxy)所在行
页面再次跳转,分别填写名称,代理地址及缓存失效时间即可(Cache统一设置为200天 288000)。
按照下表增加代理源,下图中我只设置了aliyun的配置;
1. aliyun
http://maven.aliyun.com/nexus/content/groups/public
2. apache_snapshot
https://repository.apache.org/content/repositories/snapshots/
3. apache_release
https://repository.apache.org/content/repositories/releases/
4. atlassian
https://maven.atlassian.com/content/repositories/atlassian-public/
5. central.maven.org
http://central.maven.org/maven2/
6. datanucleus
http://www.datanucleus.org/downloads/maven2
7. maven-central (安装后自带,仅需设置Cache有效期即可)
https://repo1.maven.org/maven2/
8. nexus.axiomalaska.com
http://nexus.axiomalaska.com/nexus/content/repositories/public
9. oss.sonatype.org
https://oss.sonatype.org/content/repositories/snapshots
10.pentaho
https://public.nexus.pentaho.org/content/groups/omni/
回到仓库列表,点击maven-public 将上述添加的代理加入Group,最好将默认的maven库放到最底下;
私服使用
修改本地maven中setting.xml配置文件如下,下面提供了一个修改的例子
<?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>${user.home}/.m2/repository</localRepository>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin23</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>LocalDemoMirror</id>
<mirrorOf>*</mirrorOf>
<name>LocalDemo Repository Mirror.</name>
<url>http://localhost:8081/nexus/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>LocalDemo</id>
<repositories>
<repository>
<id>nexus</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<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>
</profiles>
<activeProfiles>
<activeProfile>LocalDemo</activeProfile>
</activeProfiles>
</settings>
至此,我们就有了一个我们自己的maven私服。
你看,奇怪的知识是不是又增加了!