注意:搭建过程中使用的都是默认的配置,需根据自己的配置按照规则进行对应的修改。
Nexus
是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问 和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。
文章目录
- 1.Maven
- 1.1. Maven下载
- 1.2. Maven配置
- 2. Nexus
- 2.1. 下载
- 2.2. 配置
- 3. Nexus Repository Manager
- 3.1 登录Nexus
- 3.2 仓库介绍
- 4. Maven私有库
- 4.1 使用仓库
- 4.1.1 setting.xml
- 4.1.2 测试
- 4.2. 发布Jar包到仓库
- 4.2.1 setting.xml
- 4.2.2. 工程配置
- 5. 引用包
- 6. 问题整理
1.Maven
1.1. Maven下载
- 下载地址:https://maven.apache.org/download.cgi
- 网盘地址:https://pan.baidu.com/s/1DuE4oD4PKCy8YUzuyiViAA 提取码:
yxj2
1.2. Maven配置
- 通过服务器连接工具(个人使用的是
FileZilla
)将下载后的压缩包放入服务器目录中, - 定位到所在目录后使用
tar -zvxf apache-maven-3.6.3-bin.tar.gz
解压压缩包。 - 执行
vim /etc/profile
打开系统环境变量配置文件,点击键盘i
键进入编辑模式 - 在后面输入
export MAVEN_HOME=/root/MavenRepository/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin
- 按
Esc
退出编辑模式,输入:wq
保存并退出 - 执行
source /etc/profile
使环境变量起效。 - 执行命令
mvn -v
测试Maven环境变量是否配置成功
2. Nexus
2.1. 下载
- 下载地址:https://www.sonatype.com/nexus/repository-oss-download 网盘地址:https://pan.baidu.com/s/1DuE4oD4PKCy8YUzuyiViAA 提取码:
yxj2
未登陆时需要使用邮箱地址,选择no,输入后选择unix版本。然后等待下载,网速不好时可能会失败。如图:
2.2. 配置
- 通过服务器连接工具(个人使用的是
FileZilla
)将下载后的压缩包放入服务器目录中,定位到放置的目录后执行tar -zxvf nexus-3.29.2-02-unix.tar.gz
解压 - 解压后会得到两个文件夹
nexus-3.29.2-02
、sonatype-work
- 进入nexus的bin目录,执行
./nexus run &
运行nexus服务,&
是为了显示日志
注意:如果Linux配置较低,我个人的是1核2G,启动时会报异常,异常信息如下:
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Starting nexus
[root@iz2ze1wbnx7ym1n6p9s0upz bin]# ./nexus run &
[1] 13583
[root@iz2ze1wbnx7ym1n6p9s0upz bin]# WARNING: >************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Java HotSpot(TM) 64-Bit Server VM warning: INFO: >os::commit_memory(0x000000074f550000, 1890254848, 0) failed; >error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1890254848 bytes for >committing reserved memory.
# An error report file with more information is saved as:
# /root/MavenRepository/nexus/nexus-3.29.2-02/hs_err_pid13583.log
[1]+ Exit 1 ./nexus run
错误原因:Nexus配置的启动内存大于实际内存
解决方案:在nexus的bin目录中执行vi nexus.vmoptions
打开配置文件,如下:
mo默认未为2073m,这里修改为较小值-Xms62m -Xmx128m -XX:MaxDirectMemorySize=128m
,修改后重新执行启动命令即可成功。
3. Nexus Repository Manager
3.1 登录Nexus
在浏览器中输入http://ip地址:port
,nexus默认端口号为8081
看的很多文章说默认密码为admin
admin123
,可能是因为版本问题,Nexus3版本用户名为admin
,第一次登录为动态密码。在sonatype-work\nexus3
下有一个admin.password文件,打开后有一串字符串就是密码.(不过我修改过密码后这个文件就自动删除了好像)。
输入登录后会提示是否修改密码,可以修改成自己想要的。
3.2 仓库介绍
-
maven-central
:中央仓库。从远程中央仓库下载的可以放到此库中 -
maven-public
:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。 -
maven-releases
:私库。放置发行版本的jar包 -
maven-snapshots
:私库。放置测试版本的jar包
注意:可以自定义新建库、库的权限等。
4. Maven私有库
4.1 使用仓库
4.1.1 setting.xml
在setting.xml中添加仓库地址,:
<!--配置私服仓库地址-->
<mirror>
<id>maven-public</id>
<name>maven-public</name>
<url>http://ip:port/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<!--配置阿里云镜像地址,如果私服仓库中没有,将从阿里云仓库中获取,同时上传至私有仓库-->
<mirror>
<id>alimaven</id>
<name>aliyun Mirror</name>
<mirrorOf>central</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<profile>
<id>nexus</id><!--与下面activeProfile值对应-->
<repositories>
<repository>
<id>maven-central</id>
<name>Repository for me</name>
<url>http://ip:port/repository/maven-central/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-central</id>
<name>PluginRepository for me</name>
<url>http://ip:port/repository/maven-central/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--使用profile定义仓库需要激活才可生效-->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
注意:
ip:port
为nexus服务的IP地址和端口号,具体地址根据自己的Ip
、Port
、仓库名
组成。
4.1.2 测试
新建Maven工程,pom.xml为默认配置:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
如果本地repository仓库repository\org\apache\maven\plugins
中有maven-clean-plugin
,先删除,然后在工程目录中执行mvn clean
,此时在Nexus控制台中可以看到此插件存在(个人因为已使用,所以会有其他的依赖项):
4.2. 发布Jar包到仓库
4.2.1 setting.xml
在上述基础上,在setting.xml中添加用户名密码:
<server>
<id>maven-releases</id>
<username>用户名</username>
<password>密码</password>
</server>
<server>
<id>maven-snapshots</id>
<username>用户名</username>
<password>密码</password>
</server>
4.2.2. 工程配置
- 添加仓库地址
在pom.xml中添加
<packaging>jar</packaging><!--打包为jar后才能在其他工程中作为依赖项-->
<distributionManagement>
<repository>
<id>maven-snopshots</id>
<url>http://ip:port/repository/maven-snopshots/</url>
</repository>
</distributionManagement>
- 新建Util.java
/**
* Author: Huchx
* Date: 2021/1/27 11:05
* 用于作为依赖时测试
*/
public class Util {
public static void print(){
System.out.println("这是基础Jar包的方法");
}
}
- 上传包
执行mvn deploy
命令打包并上传到仓库,出现BUILD SUCCESS
表示成功:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.529 s
[INFO] Finished at: 2021-01-27T14:43:21+08:00
[INFO] ------------------------------------------------------------------------
- 查看包
进入Nexus Repository Manager平台查看maven-snopshots
(仓库名为pom.xml文件中设置的仓库)
5. 引用包
- 新建工程,在pom.xml中添加:
<!--如果本地没有自己发布的jar包,需要添加此项-->
<repositories>
<repository>
<id>maven-snopshots</id>
<url>http://ip:port/repository/maven-snopshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.huchx</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 然后在IDEA中可以看到
- 在启动类中添加:
public static void main( String[] args )
{
Util.print();
System.out.println( "Hello World!" );
}
- 启动后可以看到,打印jar包中的方法,测试成功:
6. 问题整理
-
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x000000074f550000, 1890254848, 0) failed; error='Cannot allocate memory' (errno=12)
错误原因:由于Linux配置较低,默认的启动内存大于机器剩余内存,所以出现错误
解决方案: 打开nexus-3.29.2-02/bin
中的nexus.vmoptions
文件,修改前两行为:-Xms62m -Xmx128m -XX:MaxDirectMemorySize=128m
,默认值为2073m
-
第一次启动后,再通过命令启动没有异常信息,但是看不到启动的进程
错误原因:不确定是否是异常,没有具体测试。
解决方案:打开nexus-3.29.2-02/bin
中的nexus
文件,设置INSTALL4J_JAVA_PREFIX ="$JAVA_HOME"
,其中$JAVA_HOME
为引用jdk的环境变量,所以必须设置jdk环境变量 - 登录Nexus Repository Manager时提示
Incorrect username or password, or no permission
错误原因:Nexus3版本使用的动态密码,不是默认的admin123,用户名还是admin
解决方案:打开sonatype-work\nexus3
目录下的admin.password
(snoatype-work
目录为Nexus压缩包解压后的其中一个),里面的字符串为首次登陆密码,登陆成功后会提示是否修改密码,这时可以自定义密码。(自定义后admin.password
文件就没有了,原因待测试) - Maven仓库地址切换为私服仓库地址后,新建Maven项目时,会停留在
Generating project in Interactive mode
相对较长时间,可能是因为网速慢。可以执行mvn archetype:generate -X -DgroupId=org.seckill -DartifactId=seckill -Dversion=1.0-SNAPSHOT
查看日志
搭建的过程中意外的发现也可以使用Nexus搭建Git私有仓库😏😏