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>
相关推荐
kinlon.liu8 分钟前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
王哲晓29 分钟前
Linux通过yum安装Docker
java·linux·docker
java66666888833 分钟前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存34 分钟前
源码分析:LinkedList
java·开发语言
执键行天涯35 分钟前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
Adolf_19931 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
Jarlen1 小时前
将本地离线Jar包上传到Maven远程私库上,供项目编译使用
java·maven·jar
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
叫我:松哥1 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
Reese_Cool1 小时前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言