Maven实战

编程入门 行业动态 更新时间:2024-10-11 13:24:02

Maven<a href=https://www.elefans.com/category/jswz/34/1769775.html style=实战"/>

Maven实战

Maven实战-私服搭建详细教程

1、为什么需要私服

首先我们为什么需要搭建Maven私服,一切技术来源于解决需求,因为我们在实际开发中,当我们研发出来一个

公共组件,为了能让别的业务开发组用上,则搭建一个远程仓库很有必要,写完公用组件后,直接发布到远程仓

库,别人需要用到时,直接从远程仓库拉取即可,而你升级组件后,只需要再发布一个新版本即可。

除此之外,如果大公司中开发人员较多,大家同时去远程仓库将依赖下载到本地,那么对公司的带宽会造成很大的

压力。很有可能会造成其他的问题。所以可以在公司的局域网内部去搭建一台服务器,开发人员所有的依赖去这台

服务器中去访问,如果该台服务器中也没有该依赖,那么该服务器就去远程仓库查找,然后下载到该服务器,最后

在返给开发者。

maven仓库管理软件:

  • Apache基金会的archiva:

  • JFrog的Artifactory:

  • Sonatype的Nexus:

推荐使用第三种,Maven私服技术,最常用的就是基于Nexus来搭建。

2、搭建Nexus私服

2.1 Nexus下载和启动

注意:nexus是java开发,所以需要保证电脑安装了java环境。

下载地址:

会有三个平台的下载地址:

# Unix archive	
.61.0-02-unix.tar.gz# Windows archive	
.61.0-02-win64.zip# Mac OS archive	
.61.0-02-mac.tgz

这里我们下载windows平台下的3.37版本,比较稳定。

下载之后放到某一个目录之后对该软件包进行解压:

nexus-3.37.3-02:里面会放Nexus启动时所需要的依赖、环境配置。

sonatype-work:存放Nexus运行时的工作数据,如存储上传的jar包等。

我们可以打开 D:\DecompressionSoftwareInstall\nexus\nexus-3.37.3-02-win64\nexus-3.37.3-02\etc

目录下的 nexus-default.properties个文件,修改一些默认配置,默认端口号是8081,如果你这个端口已被使

用,就可以修改,通常不需要更改。

进入bin目录:

打开cmd 终端,执行启动命令:

nexus.exe /run nexus

时间会比较长,耐心等待一段时间后,如果看到如下:

表示启动成功,Nexus初次启动后,会在sonatype-work目录中生成一个/nexus3/admin.password文件,这里

面存放着你的初始密码,默认账号就是admin,在浏览器输入:

# 8081端口取决于自己的机器
http://localhost:8081

访问Nexus界面,通过初始密码登录,登录后修改密码,改完密码记得重新登录一次,否则后面的操作会没有权

限。

点击右上角登录:

用户名:admin(默认)

密码:第一次启动时生成,(在 /sonatype-work/nexus3/admin.password 文件中)

登录之后可以进行一些修改密码等指引操作。

重新登录并且认证之后可以看到如下界面:

2.2 maven仓库

maven仓库既可以从中央仓库拉取依赖,也可以将本地依赖直接发到nexus中,那么就少不了maven中的仓库。

登录成功后,点击Repositories会看到一些默认仓库,每个字段含义如下:

Name: 仓库的名字
Type: 仓库的类型
Format: 仓库的格式
Status: 仓库的状态
URL: 仓库的网络地址

仓库总共分为四种类型,如下:

类型释义作用
hosted宿主仓库保存中央仓库中没有的资源,如自研组件
proxy代理仓库配置中央仓库,即镜像源,私服中没有时会去这个地址拉取
group仓库组用来对宿主、代理仓库分组,将多个仓库组合成一个对外服务
virtual虚拟仓库并非真实存在的仓库,类似于MySQL中的视图

仓库的关系如下:

注意的是,本地的Maven需要配置私服地址,当项目需要的依赖,在本地仓库没有,就会去到相应的宿主/远程仓

库拉取;如果宿主仓库也没有,就会根据配置的代理仓库地址,去到中央仓库拉取。

2.2.1 代理仓库

代理仓库主要是让用户通过代理仓库访问外部第三方仓库,如maven中央仓库、阿里的maven仓库。代理仓库会

从被代理的仓库(maven中央仓库、阿里的maven仓库)中下载依赖,缓存在代理仓库中以便让maven用户使

用。

创建代理仓库过程:

选择 Create repository:

选择 maven2(proxy):

输入远程仓库信息:

第一个红框:仓库名称

第二个红框:Release(表示从仓库中下载稳定的构建)

第三个红框:仓库地址(

然后点击创建,完成后如下:

2.2.2 宿主仓库

宿主仓库主要是供给自己使用:

1、将私有的一些构建通过网页的方式上传到宿主仓库中供大家使用。

2、将自己开发好的一些构建发布到nexus的宿主仓库中供大家使用

2.2.3 仓库组

仓库组既然是组的概念,说明它里面可以包含多个仓库。

因为maven用户可以从代理仓库和宿主仓库中下载构建至本地仓库,为了方便从代理仓库和宿主仓库下载构建,

maven提供了仓库组。

仓库组可以包含多个宿主仓库和代理仓库,maven用户访问一个仓库组就可以访问该仓库下的所有仓库。

仓库组中的多个仓库是有顺序的,当maven用户从仓库组中下载构建时,会按顺序在仓库组中查找组件,查到了

就返回给本地仓库,所以一般将速度快的放前面。

仓库组内部实际是没有内容的,只是起到一个请求转发的作用,将maven用户的下载请求转发给其它仓库处理。

nexus默认有仓库组maven-public:

打开如下:

仓库组对外url:本地maven可以通过这个url从仓库组中下载构件至本地仓库。

仓库组中成员:maven-releases(宿主的releases仓库版本)、maven-snapshots(宿主快照版本的仓库)、

maven-central(maven社区的中央仓库的代理)。

刚刚新增的maven-aliyun在左边,需要将它迁移到右边来,并且放在第三个位置,因为上述说过仓库组的查找是

有顺序的,需要将速度快的放在前面,这个新增的仓库速度比maven-central速度要快。

如下:

点击save保存。

3、本地Maven下载构建

先搭建一个新项目。

找到仓库组提供的url:

3.1 pom.xml方式

在新建项目的 pom.xml 文件中加入如下内容:

<repositories><repository><id>maven-nexus</id><url>http://localhost:8081/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository>
</repositories><!--该配置是为了防止pom中的jar包从私服下载之后,执行mvn命令插件还是从中央仓库中下载-->
<pluginRepositories><pluginRepository><id>maven-nexus</id><url>http://localhost:8081/repository/maven-public/</url></pluginRepository>
</pluginRepositories>

因为nexus需要有用户名和密码才能访问,所以需要在 setting.xml 文件中配置账号密码:

<server><id>maven-nexus</id><username>admin</username><password>admin</password>
</server>	

注意:setting.xml 中的 id 需要和 pom.xml 文件中配置的 id 一致,username和password是自己在nexus中配置

的。

注意事项:

1、 自己在pom.xml文件中配置了私服并且也在idea的maven中配置了settings.xml文件,但是jar包还是从阿里云

下载。原因:自己电脑上有好几个setting.xml文件,默认会找到settings.xml文件,自己当初指定的的xml文件名

称并不是settings.xml文件名。

解决方案:将其它的xml文件删掉或改名,并将自己指定的xml文件改名为settings.xml文件。

2、jar包从私服下载之后,但是执行mvn命令插件还是从中央仓库中下载。

原因:所有的pom文件都继承了super pom:

解决方案:在自己的pom文件中添加如下:

<pluginRepositories><pluginRepository><id>maven-nexus</id><url>http://localhost:8081/repository/maven-public/</url></pluginRepository>
</pluginRepositories>

执行mvn compile:

$ mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven hello world 1.0-SNAPSHOT                                 
[INFO] ------------------------------------------------------------------------
Downloading: http://localhost:8081/repository/maven-public//org/apache/maven/plugins/maven-jar-plugin/3.2.2/maven-jar-plugin-3.2.2.pom
Downloaded: http://localhost:8081/repository/maven-public//org/apache/maven/plugins/maven-jar-plugin/3.2.2/maven-jar-plugin-3.2.2.pom (0 B at 0.0 K
B/sec)
Downloading: http://localhost:8081/repository/maven-public//org/apache/maven/plugins/maven-plugins/34/maven-plugins-34.pom
......
Downloaded: http://localhost:8081/repository/maven-public//org/codehaus/plexus/plexus-compiler-manager/2.2/plexus-compiler-manager-2.2.jar (0 B at 
0.0 KB/sec)
Downloading: http://localhost:8081/repository/maven-public//log4j/log4j/1.2.12/log4j-1.2.12.jar
Downloaded: http://localhost:8081/repository/maven-public//org/codehaus/plexus/plexus-classworlds/2.2.2/plexus-classworlds-2.2.2.jar (0 B at 0.0 KB
/sec)
Downloading: http://localhost:8081/repository/maven-public//commons-logging/commons-logging-api/1.1/commons-logging-api-1.1.jar
Downloaded: http://localhost:8081/repository/maven-public//org/codehaus/plexus/plexus-container-default/1.5.5/plexus-container-default-1.5.5.jar (0B at 0.0 KB/sec)
Downloading: http://localhost:8081/repository/maven-public//com/google/collections/google-collections/1.0/google-collections-1.0.jar
Downloaded: http://localhost:8081/repository/maven-public//org/codehaus/plexus/plexus-compiler-javac/2.2/plexus-compiler-javac-2.2.jar (0 B at 0.0 
KB/sec)
Downloaded: http://localhost:8081/repository/maven-public//com/google/collections/google-collections/1.0/google-collections-1.0.jar (0 B at 0.0 KB/
sec)
Downloaded: http://localhost:8081/repository/maven-public//junit/junit/3.8.2/junit-3.8.2.jar (0 B at 0.0 KB/sec)
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 31.553 s
[INFO] Finished at: 2023-10-14T11:30:08+08:00
[INFO] Final Memory: 9M/149M
[INFO] ------------------------------------------------------------------------

从结果看出:我们的依赖已经从我们私服maven-nexus中下载了。

3.2 镜像方式

镜像方式就不需要在pom文件中写多余的配置,主要是配置setting.xml文件。

3.2.1 修改配置文件

Maven要使用私服,需要修改 settings.xml 文件。修改settings.xml里的镜像源配置,之前配的阿里云镜像不能用

了,改成如下:

<mirror><id>nexus-maven</id><mirrorOf>*</mirrorOf><name>Nexus Maven</name><url>http://localhost:8081/repository/maven-public/</url>
</mirror>
3.3.2 在私服中修改访问权限,允许匿名用户访问

3.3.3 在私服中配置代理仓库地址,即配置镜像源

如果已经配置了可用的代理仓库并且把它加入到maven-public组中则不需要配置了。

否则的话需要进行下面的配置:

将上述地址换成:

# 阿里云

点击Save保存即可。

3.3.4 在Maven的settings.xml中,配置私服的账号密码

<servers> 标签下,配置如下:

<server><id>nexus-maven</id><username>admin</username><password>admin</password>
</server><server><id>maven-test-release</id><username>admin</username><password>admin</password>
</server><server><id>maven-test-snapshot</id><username>admin</username><password>admin</password>
</server>
3.3.5 项目配置私服

前面配置了本地Maven与私服的关系,接着要配置项目和私服的连接。

1、为项目创建对应的私服仓库,如果已有仓库,可以直接复用。

我们要注意的就是仓库格式,有三个可选项:

Release:稳定版,表示存放可以稳定使用的版本仓库;

Snapshot:快照版,代表存储开发阶段的版本仓库;

Mixed:混合版,不区分格式,表示混合存储代码的仓库。

为了规范性,建议是Release、Snapshot格式的仓库,各自都创建一个。

2、在Maven工程的pom.xml文件中,配置对应的私服仓库地址。

<!-- 配置当前工程,在私服中保存的具体位置 -->
<distributionManagement><repository><!-- 这里对应之前 settings.xml 里配置的server-id --><id>maven-test-release</id><!-- 这里代表私服仓库的地址,大家只需要把后面的名字换掉即可 --><url>http://localhost:8081/repository/maven-test-release/</url></repository><snapshotRepository><id>maven-test-snapshot</id><url>http://localhost:8081/repository/maven-test-release/</url></snapshotRepository>
</distributionManagement>

3、将当前项目发布到私服仓库

这里可以执行mvn clean deploy命令,也可以通过IDEA工具完成。

本文的pom.xml文件的配置:

<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>hello-maven</artifactId>
<version>1.0-RELEASE</version>
<name>maven hello world</name>
# 使用IDEA工具
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building maven hello world 1.0-RELEASE
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ hello-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running org.apache.HelloWorldTest
init
Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.05 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 1[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-maven ---
[INFO] Building jar: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar
[INFO] 
[INFO] --- maven-shade-plugin:3.2.4:shade (default) @ hello-maven ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE-shaded.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-maven ---
[INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-RELEASE\hello-maven-1.0-RELEASE.jar
[INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\pom.xml to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-RELEASE\hello-maven-1.0-RELEASE.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ hello-maven ---
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.jar
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.jar (4 KB at 30.2 KB/sec)
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.pom
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.pom (4 KB at 83.6 KB/sec)
Downloading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml
[IJ]-1-METADATA_DOWNLOADED-[IJ]-path=D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\maven-metadata-maven-test-release.xml-[IJ]-artifactCoord=-[IJ]-error=Could not find metadata org.example:hello-maven/maven-metadata.xml in maven-test-release (http://localhost:8081/repository/maven-test-release/)
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml (314 B at 8.1 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.594 s
[INFO] Finished at: 2023-10-14T17:44:49+08:00
[INFO] Final Memory: 21M/209M
[INFO] ------------------------------------------------------------------------
# 使用mvn deploy命令
$ mvn clean deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven hello world 1.0-RELEASE
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running org.apache.HelloWorldTest
init
Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.047 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 1[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-maven ---
[INFO] Building jar: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar
[INFO]
[INFO] --- maven-shade-plugin:3.2.4:shade (default) @ hello-maven ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-maven ---
[INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\target\hello-maven-1.0-RELEASE.jar to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-RELEASE\hello-maven-1.0-RELEASE.jar
[INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-私服搭建详细教程\HelloMaven\pom.xml to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-RELEASE\hello-maven-1.0-RELEASE.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ hello-maven ---
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.jar
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.jar (4 KB at 32.0 KB/sec)
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.pom
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/1.0-RELEASE/hello-maven-1.0-RELEASE.pom (4 KB at 77.9 KB/sec)
Downloading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml
Uploading: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml
Uploaded: http://localhost:8081/repository/maven-test-release/org/example/hello-maven/maven-metadata.xml (314 B at 7.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.535 s
[INFO] Finished at: 2023-10-14T18:19:10+08:00
[INFO] Final Memory: 21M/214M
[INFO] ------------------------------------------------------------------------

这里要注意:

由于配置了私服上的两个宿主仓库,一个为稳定仓库,另一个为快照仓库,所以发布时,默认会根据当前项目的

<version> 版本结尾,来选择上传到相应的仓库,例如结尾是SNAPSHOT,所以会被发布到快照仓库,如果结尾

不是这个后缀时,就会被发布到Release仓库。

还有就是,你要发布的包不能带有上级,即不能有parent依赖,否则在其他人在拉取该项目时,会找不到其父项

目而构建失败。要解决这个问题,可以先将parent项目打包并上传至远程仓库,然后再发布依赖于该parent项目

的子模块。

3.3.6 Nexus配置仓库组

如果我们所处的公司,是一个大型企业,不同团队都有着各自的宿主仓库,而你如果需要其他团队的组件,这时我

们不需要在pom.xml中,将远程仓库地址先改为其他团队的地址,而是可以创建一个仓库组来解决。

1、创建仓库组

大家可以看到,图中的Members区域代表当前仓库组的成员,而这些成员会按照你排列的顺序,具备不同的优先

级,越靠前的优先级越高。创建好仓库组后,接着可以去配置一下仓库组,这里有两种方式。

2、配置单个工程与仓库组的映射

这种方式只需修改pom.xml即可:

<repositories><repository><id>maven-test</id><!-- 配置仓库组的地址 --><url>http://localhost:8081/repository/maven-test/</url><!-- 允许从中拉取稳定版的依赖 --><releases><enabled>true</enabled></releases><!-- 也允许从中拉取快照版的依赖 --><snapshots><enabled>true</enabled></snapshots></repository>
</repositories><pluginRepositories><pluginRepository><id>plugin-group</id><url>http://localhost:8081/repository/maven-test/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository>
</pluginRepositories>

在上述这组配置中,配置了<repositories><pluginRepositories>两个标签,第一个是普通依赖的仓库组

地址,第二个是插件依赖的仓库组地址,前者针对于pom.xml中的<dependency>标签生效,后者针对<plugin>

标签生效。

当你通过GAV坐标,引入一个依赖时,如果本地仓库中没找到,则会根据配置的仓库组地址,去到Nexus私服上拉

取依赖。不过因为仓库组是由多个仓库组成的,所以拉取时,会根据仓库的优先级,依次搜索相应的依赖,第一个

仓库将是最优先搜索的仓库。

3、配置本地Maven与仓库组的映射

上一种配置方式,只针对于单个Maven工程生效,如果你所有的Maven工程,都需要与Nexus私服上的仓库组绑

定,这时就可以直接修改settings.xml文件,如下:

<profile><id>maven-test-group</id><repositories><repository><id>nexus-maven</id><url>http://localhost:8081/repository/maven-test-group/</url><releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>nexus-maven</id><url>http://localhost:8081/repository/maven-test-group/</url><releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></pluginRepository></pluginRepositories>
</profile>

这组配置要写在<profiles>标签里面,其他的与前一种方式没太大区别,唯一不同的是多了一个

<updatePolicy>标签,该标签的作用是指定仓库镜像的更新策略,可选项如下:

  • always:每次需要Maven依赖时,都先尝试从远程仓库下载最新的依赖项;
  • daily:每天首次使用某个依赖时,从远程仓库中下载一次依赖项;
  • interval:X:每隔X个小时,下载一次远程仓库的依赖,X只能是整数;
  • never:仅使用本地仓库中已经存在的依赖项,不尝试从远程仓库中拉取。

Maven工程使用依赖时,首先会从本地仓库中查找所需的依赖项,如果本地仓库没有,则从配置的远程仓库下载

这时会根据<updatePolicy>策略来决定是否需要从远程仓库下载依赖。

还得激活一下上述配置:

<!-- seetings.xml文件配置 -->
<activeProfiles><!-- 这里写前面配置的ID --><activeProfile>maven-test-group</activeProfile>
</activeProfiles>

不过要记住,无论两种方式的哪一种,都只允许从私服上拉取依赖,如果你的某个工程,想要打包发布到私服上,

还是需要配置前面说的<distributionManagement>标签。

更多推荐

Maven实战

本文发布于:2023-12-03 10:43:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1654470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实战   Maven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!