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

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

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

一、创建 maven 项目

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

在这里插入图片描述

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

在这里插入图片描述

二、编写简单的API功能

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

在这里插入图片描述

三、打包发布到本地仓库

1、配置 maven 中的 settings.xml 文件

打包发布到本地仓库,那么必须要先创建好本地仓库。在本地,只需要在 maven 安装目录下的 settiings.xml 文件中配置本地仓库 localRepository 所在的目录即可!!

<?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、打包发布到本地仓库
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 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、打包发布到远程仓库
mvn package deploy # 执行该命令,会将项目打包成jar包依赖,并发布到远程仓库

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

在这里插入图片描述

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

在这里插入图片描述

4、测试

在本地项目中,通过该远程 jar 包依赖的坐标进行引入,测试能否正常使用!

注意:这里的测试,需要先删除上一步中本地仓库安装的 jar 包依赖!!!这样才能够测试出能否拉取远程仓库中的 jar 包依赖!

4.1、配置 maven 中的 settings.xml 文件

同样的,要想从远程仓库中拉取项目所需要的 jar 包依赖,同样需要告诉 maven 工具从哪个远程仓库上拉取依赖?访问远程仓库的用户和密码是多少?

<?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 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 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>
  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
idea是一种集开发环境(IDE),能够帮助开发者更轻松地开发、调试和部署应用程序。Maven是一个项目管理工具,它提供了一种方便的方式来管理和构建Java项目,并且有一个仓库来存储各种依赖项。在使用idea上传jarMaven仓库时,可以按照以下步骤进行操作: 1. 在Maven项目的pom.xml文件添加jar包的相关配置信息。例如,可以在<dependencies>标签添加一个<dependency>标签,指定jar包的坐标、版本号等信息。 2. 在项目根目录下执行Maven命令,将jar打包并上传到Maven仓库。可以使用Maven插件或命令行工具,在命令行执行类似于"mvn clean install"的命令,将jar打包标准的Maven构建产物,并将其上传到本地Maven仓库远程仓库。 3. 配置Maven仓库的地址和认证信息。如果使用的是远程仓库,需要在settings.xml文件配置远程仓库的地址,并提供认证信息以获得上传权限。 4. 使用ideaMaven插件进行构建和上传。在ideaMaven面板,可以找到相关的命令和配置选项,可以使用插件来构建项目并上传jar包到Maven仓库。 以上是大致的步骤,具体操作可能会因开发环境和项目配置而有所不同。但总的来说,使用idea上传jarMaven仓库是一个相对简单的过程,只需要在项目配置添加必要的依赖信息,并使用Maven命令或插件进行打包和上传即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总是提示已注册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值