简单的maven nexus私服学习
1.需求
我们现在使用的maven私服是之前同事搭建的,是在公司的一台windows电脑上面,如果出问题会比较难搞,所以现在想将私服迁移到我们公司的测试服务器上,此处简单了解一下私服的一些配置记录一下,方便以后的学习使用。
2.简介
Maven Nexus 私服是一种企业级的仓库管理系统,主要用于管理Maven项目的依赖关系和构建产物。Nexus是由Sonatype公司开发的,它可以作为一个中心化的仓库来存储和分发各种类型的构建工件,包括但不限于Maven、Gradle、npm、Docker等。
Maven Nexus 私服的主要功能包括:
- 依赖管理:代理外部仓库:Nexus可以作为代理,帮助项目从外部仓库(如Maven Central)下载依赖,同时缓存这些依赖以提高下载速度。
- 本地仓库:项目可以将构建的工件发布到Nexus的本地仓库,供其他项目或团队成员使用。
- 版本控制:快照和发布版本管理:Nexus可以区分快照版本(SNAPSHOT)和正式版本(RELEASE),并提供相应的生命周期管理功能。
- 安全性:身份验证和授权:可以配置Nexus来保护仓库资源,只有经过身份验证和授权的用户才能访问或上传工件。
- 监控和报告:工件版本历史记录:提供详细的工件版本历史记录,方便追踪工件的变化。
- 依赖分析:帮助识别项目中使用的依赖及其版本,以及潜在的问题,如过时的依赖或安全漏洞。
- 集成能力:与其他工具集成:Nexus可以轻松地与CI/CD工具(如Jenkins)、IDEs和其他构建工具集成。
- REST API:提供了丰富的RESTful API来自动化仓库操作。
- 用户界面:友好的UI,Nexus提供了一个直观的用户界面,便于管理员和开发者进行仓库管理和工件浏览。
Maven Nexus 私服使用场景:
- 内部项目依赖管理:在一个组织内部,不同的开发团队可能会共享一些通用库或组件,通过Nexus可以集中管理这些内部依赖。
- 隔离外部网络:对于无法直接访问互联网的企业环境,Nexus可以作为一个内部的代理,使得开发人员无需直接连接到外部仓库即可获取依赖。
- 构建产物发布:除了管理依赖之外,Nexus还可以用来发布和管理应用的不同版本,包括快照版本和最终发布的版本。
通过使用Nexus作为Maven私服,可以有效地提高开发效率,减少网络带宽消耗,并加强依赖管理的安全性和可控性。
3.私服安装
3.1 下载应用包
我们首先需要下载私服包,具体的下载地址,找到自己需要的版本下载即可
3.2 上传并且解压
上传包到自定义目录,我使用的是/root/nexus
目录下,然后进行解压,然后进入到 /root/nexus/nexus-3.49.0-02/etc
目录下,然后修改默认启动端口信息,解压后有两个目录信息nexus-3.49.0-02
以及sonatype-work
,两个目录分别为程序文件以及存储信息目录。
tar -zxvf nexus-3.49.0-02-unix.tar.gz
cd /root/nexus/nexus-3.49.0-02
vim nexus-default.properties
3.3 启动服务
我们主要进行的是nexus
的私服学习,所以此处就是简单启动即可,没有设置为开机自启动。具体步骤我们进入到nexus/bin
目录中,执行启动脚本即可
./nexus start
查看是否已经启动成功,查看进程启动后代表启动成功,启动之后便可以使用ip:8888
访问私服了,首次启动稍微有点慢,稍微等一会儿便可以访问
ps -ef | grep nexus
我们具体的访问地址为 http://192.168.138.134:8888/
,首次登录我们需要使用admin登录,然后admin用户的密码我们可以在 /sonatype-work/nexus3/admin.password
中获取,然后更新密码即可
我们点击登录,使用admin用户进行登录,之后我们便可以进行私服的创建了
4. 创建私服
4.1 私服的种类
常见的使用的仓库类型有三种,每种仓库作用如下:
类型 | 作用描述 |
---|---|
group(仓库组类型) | 整合多个仓库,统一对外暴露服务地址,方便用户使用,不需要用户配置多个仓库地址 |
proxy(代理类型) | 代理仓库,如果有坐标请求到代理仓库,仓库首先确定仓库本地是否存在,存在则返回,不存在则去配置代理地址进行坐标获取 |
hosted(宿主类型) | 用户自定义仓库,用户可以将自己的本地仓库部署到此仓库中 |
4.2 私服命名含义
仓库名称一般都有含义,一般名称含义如下:
名称 | 名称含义 |
---|---|
central | maven中央库,默认从https://repo1.maven.org/maven2/ 获取坐标 |
release | 用户私服的稳定发型版本 |
snapshot | 用户私服快照版本 |
public | 一般将上方的central,release,snapshot三个版本进行整合,在maven的setting.conf配置文件中配置地址,统一对外提供服务 |
4.3 私服创建
我们了解了私服的种类以及名称含义,那么我们可以本地尝试新建几个仓库,进行测试使用,我们分别建立hosted类型,proxy类型以及group类型仓库,具体的步骤如下:
-
proxy 代理仓库
我们新增maven-ali-proxy[阿里云代理仓库
],点击 新增仓库
然后选择maven2
下的自己需要的仓库种类proxy
然后填写需要的代理私服名称以及代理私服地址信息
点击View certificate
按钮,查看服务器的SSL证书信息。
最后点击save
按钮保存即可
这样我们便建立好了一个代理仓库 -
hosted 宿主仓库
我们开始建立一个宿主仓库,宿主仓库我们建立我们spring boot 应用包上传的仓库,包含snapshot以及release,此处我们以snapshot仓库为例,进行仓库建造,首先还是点击 新增仓库按钮
然后选择maven2
下的自己需要的仓库种类hosted
填写名称等基本信息
最后点击保存 save 按钮即可
完成snapshot版本仓库后,再使用相同的方法再次新增 release 版本仓库 -
group 仓库
首先同样是点击 新增仓库按钮
然后选择maven2
下的自己需要的仓库种类group
然后填写名称信息等基本信息
最后选择其整合的仓库列表信息,选择好后则点击 save按钮 进行保存即可
这样我们的几个自定义的仓库便建立好了,那我们在仓库管理页面查看一下
5. 仓库的使用
在上面新增仓库之后,我们便可以使用新增的仓库了,我们使用的是gourp
类型的仓库对外提供服务,我们本地需要配置好jdk环境以及maven环境,再配置自定义的maven setting.conf 文件,然后项目中便可以从库中引入pom坐标了,具体的实现如下
5.1 java & maven环境
我们需要先配置java 以及 maven 环境,我已经准备好了
5.2 idea 配置maven
其中 setting-local-nexus.xml
主要配置了server连接信息,以及minor镜像信息,其中server连接信息里面的id 需要与 pom文件中的 distributionManagement.repository.id 保持一致,这样才能上传本地jar包到私服中,里面的配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\apache-maven-3.6.3\repos_local_nexus</localRepository>
<!--<localRepository>D:\old computer\Snxxxt_new\repository\LLNXWFWHTReprository</localRepository>-->
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<servers>
<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<server>
<id>group</id>
<username>admin</username>
<password>101022li</password>
</server>
<server>
<id>release</id>
<username>admin</username>
<password>101022li</password>
</server>
<server>
<id>snapshot</id>
<username>admin</username>
<password>101022li</password>
</server>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</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>
-->
</servers>
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- 配置远程仓库 -->
<mirror>
<id>maven-git-group</id>
<name>nexus repository</name>
<url>http://192.168.138.134:8888/repository/maven-git-group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!--
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
-->
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<!-- 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>
-->
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
如果想要上传代码到私服,需要添加maven deploy 插件,我的项目的pom文件内容配置具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.git</groupId>
<artifactId>docker-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis-plus.version>3.3.0</mybatis-plus.version>
<fastjson.version>1.2.83</fastjson.version>
<druid.version>1.2.4</druid.version>
<hutool.version>5.5.7</hutool.version>
<lombok.version>1.18.6</lombok.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
<swagger.version>3.0.0</swagger.version>
<elasticjob.version>3.0.0-RC1</elasticjob.version>
<druid.version>1.2.4</druid.version>
<poi-tl.version>1.9.1</poi-tl.version>
<poi.version>4.1.2</poi.version>
<easyexcel.version>2.2.8</easyexcel.version>
</properties>
<!-- springboot dependency -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
<relativePath/>
</parent>
<!-- 项目级别的maven配置 -->
<repositories>
<repository>
<id>maven-git-group</id>
<name>maven-git-group</name>
<url>http://192.168.138.134:8888/repository/maven-git-group/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<!-- 部署jar包到私服配置,包含快照版本以及release版本 -->
<distributionManagement>
<repository>
<id>release</id>
<name>maven-git-release</name>
<url>http://192.168.138.134:8888/repository/maven-git-release/</url>
</repository>
<snapshotRepository>
<id>snapshot</id>
<name>maven-git-snapshot</name>
<url>http://192.168.138.134:8888/repository/maven-git-snapshot/</url>
</snapshotRepository>
</distributionManagement>
<dependencyManagement>
<dependencies>
<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 结巴分词 -->
<dependency>
<groupId>com.huaban</groupId>
<artifactId>jieba-analysis</artifactId>
<version>1.0.2</version>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!-- 远程执行shell -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<!-- ftp上传下载-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- package -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- maven私服jar包部署插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
6. 测试
配置完毕后,我们点击 spring boot 项目 maven 的 install 即可,执行后效果如下,发现代码已经成功的打包了,我们去maven-git-group 库中查看,包已经下载好了。
点击 deploy
部署本地jar包到服务端,具体测试效果如下:
查看snapshot仓库,代码已经上传,