03.maven连接nexus私服配置以及maven settings.xml文件详解

settings.xml有什么用?

如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径。

settings.xml文件是干什么的,为什么要配置它呢?
从settings.xml的文件名就可以看出,它是用来设置maven参数的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在项目的局部配置。
Settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

settings.xml文件位置

settings.xml文件一般存在于两个位置:
全局配置: ${M2_HOME}/conf/settings.xml
用户配置: {user.home} 和和所有其他系统属性只能在3.0+版本上使用。请注意windows和Linux使用变量的区别。

配置优先级

需要注意的是:局部配置优先于全局配置。
配置优先级从高到低:pom.xml> user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。

maven怎么从远程仓库下载jar包,setting中配置:

     <!--  我们使用maven下载需要的jar包,但是很多的时候由于中央仓库没有,所以此处可以在maven的设置中心添加多个下载仓库,当中央仓库没有的话,继续到下一个仓库去下载。这样丰富了中央仓库的下载地址。  -->
     <mirror>
      <id>nexus</id>
      <name>nexus maven</name>
      <url>http://localhost:8081/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>        
     </mirror> 

     <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
     </mirror> 

     <mirror>  
      <id>repo2</id>  
      <mirrorOf>central</mirrorOf>  
      <name>Human Readable Name for this Mirror.</name>  
      <url>http://repo2.maven.org/maven2/</url>  
    </mirror>
  </mirrors>

Nexus配置
项目使用nexus私服的jar包,在项目的pom.xml文件中指定私服仓库

<repositories>
      <repository>
          <id>nexus</id>
          <name>nexus</name>
          <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
             <enabled>true</enabled>
         </snapshots>
     </repository>
 </repositories>

项目使用nexus私服的插件,在项目的pom.xml文件中指定插件仓库

 <pluginRepositories>
      <pluginRepository>
          <id>nexus</id>
          <name>nexus</name>
          <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
             <enabled>true</enabled>
         </snapshots>
     </pluginRepository>
 </pluginRepositories>

如果想本机所有的maven项目都使用私服的组件,可以在maven的设置文件settings.xml中添加属性,并激活

  <profile>
       <id>Nexus</id>
       <repositories>
         <repository>
          <id>nexus</id>
          <url>http://localhost:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
         </repository>
       </repositories>
       <pluginRepositories>
         <pluginRepository>
           <id>nexus</id>
           <url>http://localhost:8081/repository/maven-public/</url>
           <releases>
            <enabled>true</enabled>
           </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
         </pluginRepository>
        </pluginRepositories>
     </profile>
  </profiles>
  <!-- 激活 -->
  <activeProfiles>
    <activeProfile>Nexus</activeProfile>
  </activeProfiles>

如何将自己的项目发布到nexus私服

我们知道用mvn install命令可以将项目装载的本地的仓库,但是项目发布到私服,maven项目就要使用命令:mvn clean deploy;
要想发布项目到nexus里,必须通过标签来进行配置。在之前的文章中有介绍nexus的工厂类别,其中提到两个:hosted里的Releases、Snapshots.

当我们发布项目到nexus里时,如果项目版本是x.x.x-Releases,则会发布到Releases工厂中;而项目版本是x.x.x-SNAPSHOTS则发布到Snapshots工厂中。
需要在pom文件中配置一下代码;

<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/repository/maven-snapshots//</url>
        </snapshotRepository>
    </distributionManagement>

注意还需要配置mvn发布的权限,否则会报401错误,在settings.xml中配置权限,其中id要与pom文件中的id一致

  <!--授权信息  -->
      <server> 
        <id>nexus-releases</id> 
        <username>admin</username> 
        <password>admin123</password> 
      </server> 
      <server> 
        <id>nexus-snapshots</id> 
        <username>admin</username> 
        <password>admin123</password> 
      </server>

这里面的username和password对应的是nexus私服中具有发布权限的用户名和密码

参考文件:
http://blog.csdn.net/u012225679/article/details/73740785
https://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension
http://blog.csdn.net/moshenglv/article/details/52027125
http://blog.csdn.net/haohaizijhz/article/details/72841489
https://www.cnblogs.com/h–d/p/5719040.html
http://blog.csdn.net/cwh056056/article/details/49667971

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Mavensettings.xml文件是用来配置Maven的各种设置的,其中包括配置私服的设置。配置私服可以让我们在本地构建项目时,使用本地的私服来下载依赖包,从而提高构建速度。 在settings.xml文件中,我们需要添加一个<mirrors>标签,用来配置私服的镜像。具体配置如下: ``` <mirrors> <mirror> <id>my-mirror</id> <url>http://my-private-repo.com/maven2</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 其中,<id>标签用来指定镜像的ID,<url>标签用来指定私服的地址,<mirrorOf>标签用来指定需要被镜像的仓库。在这个例子中,我们使用了通配符“*”,表示所有的仓库都需要被镜像。 除了<mirrors>标签外,我们还需要添加一个<servers>标签,用来配置私服的认证信息。具体配置如下: ``` <servers> <server> <id>my-private-repo</id> <username>my-username</username> <password>my-password</password> </server> </servers> ``` 其中,<id>标签用来指定私服的ID,<username>标签用来指定私服的用户名,<password>标签用来指定私服的密码。 配置settings.xml文件后,我们就可以在本地构建项目时,使用私服来下载依赖包了。 ### 回答2: Maven是一个流行的构建工具,它可以帮助我们管理和构建项目。一个常见的情况是,我们需要将一些私有库存储在一个私服上,而不是让 Maven 去下载。 这时候,我们需要在 Maven 环境中配置私服。一个常见的配置方式是修改 Mavensettings.xml 文件。 settings.xml 文件是 Maven配置文件,它通常存储在用户主目录下的 .m2 目录中。在 settings.xml 文件中,我们可以定义一些 Maven 的全局配置,例如源代码库和私服。我们可以通过编辑这个文件,为 Maven 配置私服。 首先,我们需要找到 settings.xml 文件。如果你在使用 Maven 命令行工具,可以直接在命令行中输入以下命令: ```shell $ mvn help:effective-settings ``` 这会告诉你有效的 settings.xml 文件的位置。在这个文件中,我们可以添加以下内容来配置私服: ```xml <settings> ... <servers> <server> <id>my-nexus-repo</id> <username>your_username</username> <password>your_password</password> </server> </servers> ... <mirrors> <mirror> <id>my-nexus-repo</id> <name>my-nexus-repo</name> <url>http://your.prvate.repo/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ... </settings> ``` 在这个 XML 块中,我们配置了一个 <servers> 标签,其中添加了一个 <server> 块,定义了私服的 ID、用户名和密码。 接下来,我们在 <mirrors> 中添加一个 <mirror> 块来定义镜像。在这种情况下,我们定义了一个 ID 和 Name,并指定了我们私服的 URL。通过这个设置,Maven 将会从我们配置私服中查找库。 当我们配置settings.xml 后,我们需要在 pom.xml 文件中添加以下 XML 块来使用这个私服: ```xml <project> ... <repositories> <repository> <id>my-nexus-repo</id> <url>http://your.private.repo/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> ... </project> ``` 在这个 XML 块中,我们定义了一个 <repositories> 标记,它定义了一个 ID 和 URL,这些信息与我们之前在 settings.xml 文件中定义的一致。同时,我们设置了两个布尔值,用来启用 Maven 使用我们私服的 release 和 snapshot 库。 通过这个配置,我们就能在使用 Maven 命令下载依赖库时,从我们配置私服中查找库了。 ### 回答3: Maven是Java开发中非常流行的构建工具之一。在使用Maven构建项目时,往往需要从外部库中下载相关的依赖包。但是有些依赖包可能因为网络问题或者版本问题无法下载,这个时候就需要搭建本地私服。下面就是介绍如何在Maven配置settings.xml文件来使用私服。 一、准备工作 首先需要准备好一个私服,可以使用Sonatype Neco等开源的私服,也可以使用商业类的JFrog Artifactory等。在安装配置私服后,需要在私服中添加需要的依赖包,以供自己的项目使用。 二、编辑settings.xml配置文件 Maven配置文件位于Maven安装目录下的conf文件夹中,名为settings.xml。我们可以通过编辑该文件,来配置Maven使用私服。 1.添加<mirrors>节点 <mirrors>节点是私服配置的第一部分,它可以用来指定镜像节点。由于Maven中央库的下载速度较慢,因此我们可以通过指定镜像节点来加快下载速度。配置如下: ``` <mirrors>     <mirror>       <id>nexus</id>       <mirrorOf>*</mirrorOf>       <url>http://your-host:8081/repository/maven-public/</url>     </mirror> </mirrors> ``` 2.添加<settingsProfiles>节点 <settingsProfiles>节点是用于添加配置项的节点,它包含多个<profile>节点。我们可以通过在<profiles>节点下添加<profile>节点来指定需要使用的的私服信息。配置如下: ``` <profiles>   <profile>     <id>myprofile</id>     <repositories>       <repository>         <id>nexus</id>         <url>http://your-host:8081/repository/maven-releases/</url>         <releases>           <enabled>true</enabled>           <checksumPolicy>fail</checksumPolicy>         </releases>         <snapshots>           <enabled>true</enabled>           <checksumPolicy>warn</checksumPolicy>         </snapshots>       </repository>     </repositories>   </profile> </profiles> ``` 其中,id用来指定profile的ID,repositories节点配置了具体的私服信息,id节点指定了私服的ID,url节点指定了私服的地址,releases和snapshots节点分别指定了私服是否支持发布和快照版本。 3.添加<settingsActiveProfiles>节点 最后,我们需要添加<settingsActiveProfiles>节点来激活我们指定的profile,配置如下: ``` <activeProfiles>   <activeProfile>myprofile</activeProfile> </activeProfiles> ``` 这样配置文件就编辑完毕了,保存之后就可以使用该私服了。 三、总结 通过配置settings.xml文件,可以有效的管理本地私服,提高Maven的构建效率。同样,在多人开发或者不同项目使用不同私服时,可以根据需要修改settings.xml文件中的镜像节点和<profile>节点,灵活进行管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值