IDEA中的 Maven 项目打包成Jar依赖,如何发布到本地仓库、远程仓库?

IDEA中的 Maven 项目打包成Jar依赖,如何发布到本地仓库、远程仓库?

默认读者都已经安装好 maven环境,如有不知道如何安装 maven 环境的,请先进行 maven 环境的安装学习教程!

一、创建 maven 项目

按照图中方式,创建一个最简单的 Maven 项目!

创建的初始项目如下图所示!如果要将打包的项目发布到本地和远程 maven 仓库,则打包方式 packaging 必须为 jar 包形式!!

二、编写简单的API功能

创建一个工具类,实现简单的冒泡排序功能!

三、打包发布到本地仓库

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包依赖,并发布到本地仓库

或者如下图,依次点击 packageinstall 按钮也可!

如下图,可以看到本地仓库中已经安装好该 jar 包依赖了,在本地的任何项目中,通过 pom 文件引入该依赖的坐标位置,即可使用!!

3、测试使用

在本地的其它项目中,通过该 jar 包依赖的坐标进行引入,测试能否正常使用!如下图所示!!

如下图所示,该依赖包能够正常引入到本地项目中进行使用!发布到本地仓库成功!!

四、打包发布到远程仓库

1、创建远程仓库

打包发布到远程仓库,那么首先需要创建一个远程的 maven 仓库!这里可以使用阿里云的云效平台免费创建远程 Maven 仓库,地址:https://packages.aliyun.com/

仓库名称任意起名!!仓库地址任意起名!! release 表示存储稳定版的依赖包,snapshots 表示存储开发版的依赖包!!两个都勾选表示该仓库可以存储两种形式的依赖包!!

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包依赖,并发布到远程仓库

或者如下图,依次点击 packagedeploy 按钮也可!

如下图,可以看到远程仓库中已经拥有该 jar 包依赖了!!在任何项目中,只需要配置拉取该远程仓库中依赖的相关信息,再在 pom 文件中引入该依赖的坐标位置,即可使用!!

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 测试使用

如下图所示,该依赖包能够正常引入到本地项目中进行使用!发布到远程仓库成功!!

五、其它注意点

我们使用 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>
相关推荐
无心水31 分钟前
【分布式利器:腾讯TSF】10、TSF故障排查与架构评审实战:Java架构师从救火到防火的生产哲学
java·人工智能·分布式·架构·限流·分布式利器·腾讯tsf
一 乐8 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
Boilermaker19928 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维8 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
alonewolf_999 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子9 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
码事漫谈9 小时前
Protocol Buffers 编码原理深度解析
后端
sheji34169 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
码事漫谈9 小时前
gRPC源码剖析:高性能RPC的实现原理与工程实践
后端
恋爱绝缘体19 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit