IDEA中的 Maven 项目打包成Jar依赖,如何发布到本地仓库、远程仓库?
默认读者都已经安装好
maven
环境,如有不知道如何安装maven
环境的,请先进行maven
环境的安装学习教程!
一、创建 maven 项目
按照图中方式,创建一个最简单的
Maven
项目!
![](https://i-blog.csdnimg.cn/direct/16346c9fc0404d22947b9a55379bec9e.png)
创建的初始项目如下图所示!如果要将打包的项目发布到本地和远程
maven
仓库,则打包方式packaging
必须为jar
包形式!!
![](https://i-blog.csdnimg.cn/direct/14d94078ff404142bcb81f5abeac9980.png)
二、编写简单的API功能
创建一个工具类,实现简单的冒泡排序功能!
![](https://i-blog.csdnimg.cn/direct/67015344a1044869944d71c892bce5b2.png)
三、打包发布到本地仓库
1、配置 maven 中的 settings.xml
文件
打包发布到本地仓库,那么必须要先创建好本地仓库。在本地,只需要在
maven
安装目录下的settiings.xml
文件中配置本地仓库localRepository
所在的目录即可!!
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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/Repository/</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers></servers>
<mirrors>
</mirrors>
<profiles></profiles>
</settings>
2、打包发布到本地仓库
cmd
mvn package install # 执行该命令,会将项目打包成jar包依赖,并发布到本地仓库
或者如下图,依次点击
package
和install
按钮也可!
![](https://i-blog.csdnimg.cn/direct/c681905e2da24206b7666ac4679c0aa7.png)
如下图,可以看到本地仓库中已经安装好该
jar
包依赖了,在本地的任何项目中,通过pom
文件引入该依赖的坐标位置,即可使用!!
![](https://i-blog.csdnimg.cn/direct/36b356df4e574b0f97f95ac1e55ccd08.png)
3、测试使用
在本地的其它项目中,通过该
jar
包依赖的坐标进行引入,测试能否正常使用!如下图所示!!
![](https://i-blog.csdnimg.cn/direct/3ca742c5765b465288d0df49bebca7b4.png)
如下图所示,该依赖包能够正常引入到本地项目中进行使用!发布到本地仓库成功!!
![](https://i-blog.csdnimg.cn/direct/e17776d5fafc430885ad839a8d630f10.png)
四、打包发布到远程仓库
1、创建远程仓库
打包发布到远程仓库,那么首先需要创建一个远程的
maven
仓库!这里可以使用阿里云的云效平台免费创建远程Maven
仓库,地址:https://packages.aliyun.com/
![](https://i-blog.csdnimg.cn/direct/5643886a5c654d2aa90b223d39f3a12d.png)
仓库名称任意起名!!仓库地址任意起名!!
release
表示存储稳定版的依赖包,snapshots
表示存储开发版的依赖包!!两个都勾选表示该仓库可以存储两种形式的依赖包!!
![](https://i-blog.csdnimg.cn/direct/43ec081a491c46179e2daa0420cf3c19.png)
2、配置 maven 中的 settings.xml
文件
要将本地开发好的项目打包成
jar
包,并发布到远程仓库,那么肯定要让maven
工具知道,我们的jar
包发布的仓库地址在哪里?远程仓库的访问用户和密码是多少?因此,需要在
maven
安装目录下的settings.xml
中配置相关的信息!
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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/Repository/</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<!-- 访问远程仓库的用户名和密码 -->
<server>
<id>origin-maven-test</id>
<username>66bc59ca9a2728f7f2866860</username>
<password>你的密码</password>
</server>
</servers>
<profiles>
<profile>
<!-- 任意起名,表示这份配置的名称,需要与下面activeProfiles中相对应 -->
<id>rdc</id>
<!-- 推送jar包依赖的远程仓库配置 -->
<properties>
<!-- 推送稳定版依赖包的远程仓库地址 -->
<altReleaseDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altReleaseDeploymentRepository>
<!-- 推送开发版依赖包的远程仓库地址 -->
<altSnapshotDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altSnapshotDeploymentRepository>
</properties>
</profile>
</profiles>
<activeProfiles>
<!-- 激活使用的配置 -->
<activeProfile>rdc</activeProfile>
</activeProfiles>
</settings>
3、打包发布到远程仓库
cmd
mvn package deploy # 执行该命令,会将项目打包成jar包依赖,并发布到远程仓库
或者如下图,依次点击
package
和deploy
按钮也可!
![](https://i-blog.csdnimg.cn/direct/434b184a809243a7bed1640d32ad5423.png)
如下图,可以看到远程仓库中已经拥有该
jar
包依赖了!!在任何项目中,只需要配置拉取该远程仓库中依赖的相关信息,再在pom
文件中引入该依赖的坐标位置,即可使用!!
![](https://i-blog.csdnimg.cn/direct/8be67907e4ee41198ff1f959e607bcf5.png)
4、测试
在本地项目中,通过该远程
jar
包依赖的坐标进行引入,测试能否正常使用!注意:这里的测试,需要先删除上一步中本地仓库安装的
jar
包依赖!!!这样才能够测试出能否拉取远程仓库中的jar
包依赖!
4.1、配置 maven 中的 settings.xml
文件
同样的,要想从远程仓库中拉取项目所需要的
jar
包依赖,同样需要告诉maven
工具从哪个远程仓库上拉取依赖?访问远程仓库的用户和密码是多少?
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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/Repository/</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<!-- 访问远程仓库的用户名和密码 -->
<server>
<id>origin-maven-test</id>
<username>66bc59ca9a2728f7f2866860</username>
<password>你的密码</password>
</server>
</servers>
<profiles>
<profile>
<!-- 任意起名,表示这份配置的名称,需要与下面activeProfiles中相对应 -->
<id>rdc</id>
<!-- 推送jar包依赖的远程仓库配置 -->
<properties>
<!-- 推送稳定版依赖包的远程仓库地址 -->
<altReleaseDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altReleaseDeploymentRepository>
<!-- 推送开发版依赖包的远程仓库地址 -->
<altSnapshotDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altSnapshotDeploymentRepository>
</properties>
<!-- 拉取jar包依赖的远程仓库配置 -->
<repositories>
<repository>
<id>origin-maven-test</id>
<!-- 拉取jar包依赖的远程仓库地址 -->
<url>https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test</url>
<releases>
<!-- 可以拉取稳定版的jar包 -->
<enabled>true</enabled>
</releases>
<snapshots>
<!-- 可以拉取开发版的jar包 -->
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!-- 激活使用的配置 -->
<activeProfile>rdc</activeProfile>
</activeProfiles>
</settings>
4.2 测试使用
如下图所示,该依赖包能够正常引入到本地项目中进行使用!发布到远程仓库成功!!
![](https://i-blog.csdnimg.cn/direct/d7cf73d0638a4083b6a7d934249dad90.png)
![](https://i-blog.csdnimg.cn/direct/395b19bd9a0949638df670b0974ffb46.png)
五、其它注意点
我们使用
maven
环境时,大部分是需要下载第三方的jar
包依赖的,即熟知的maven
中央仓库!!由于maven
中央仓库的地址在国外,因此下载速率很慢!!这里我们可以使用阿里云的仓库镜像地址来进行下载!!其
settings.xml
配置阿里云仓库镜像地址如下!
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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/Repository/</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers></servers>
<mirrors>
<!-- 由阿里云提供的Maven中央仓库镜像 -->
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<!-- 表示需要使用该镜像地址进行依赖下载的仓库,*表示所有仓库中的依赖下载均使用该镜像地址 -->
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles></profiles>
</settings>
特别注意:如果我们在
settings.xml
中设置了自己的私有远程仓库,那么在配置阿里云的仓库镜像地址时,需要在<mirrorOf>*</mirrorOf>
中通过<mirrorOf>*,!仓库ID</mirrorOf>
排除自己的私有仓库,否则在下载自己私有仓库中的依赖时,也会去我们配置的镜像仓库地址中进行下载,这样是下载不到私有仓库中的jar
包依赖的!!!
最后,整体的 settings.xml
文件内容如下
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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/Repository/</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<!-- 访问远程仓库的用户名和密码 -->
<server>
<id>origin-maven-test</id>
<username>66bc59ca9a2728f7f2866860</username>
<password>你的密码</password>
</server>
</servers>
<profiles>
<profile>
<!-- 任意起名,表示这份配置的名称,需要与下面activeProfiles中相对应 -->
<id>rdc</id>
<!-- 推送jar包依赖的远程仓库配置 -->
<properties>
<!-- 推送稳定版依赖包的远程仓库地址 -->
<altReleaseDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altReleaseDeploymentRepository>
<!-- 推送开发版依赖包的远程仓库地址 -->
<altSnapshotDeploymentRepository>
origin-maven-test::default::https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test
</altSnapshotDeploymentRepository>
</properties>
<!-- 拉取jar包依赖的远程仓库配置 -->
<repositories>
<repository>
<id>origin-maven-test</id>
<!-- 拉取jar包依赖的远程仓库地址 -->
<url>https://packages.aliyun.com/66bc5f73b9168fe4f613ad66/maven/origin-maven-test</url>
<releases>
<!-- 可以拉取稳定版的jar包 -->
<enabled>true</enabled>
</releases>
<snapshots>
<!-- 可以拉取开发版的jar包 -->
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!-- 激活使用的配置 -->
<activeProfile>rdc</activeProfile>
</activeProfiles>
<mirrors>
<!-- 由阿里云提供的Maven中央仓库镜像 -->
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<!-- 表示需要使用该镜像地址进行依赖下载的仓库,*表示所有仓库中的依赖下载均使用该镜像地址,!表示排除哪些仓库 -->
<mirrorOf>*,!origin-maven-test</mirrorOf>
</mirror>
</mirrors>
</settings>