Maven的settings.xml文件详解及项目打包到maven私有仓库的方式

背景:

项目开发过程中遇到需要本地编译时要获取合作伙伴私服的jar包,所以需要在我们本地的settings文件中添加合作伙伴私有仓库配置,于是对settings文件进行了调研,现分享如下:

一、settings文件内容详解

  1. settings文件

<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>/Users/java/maven/repositorylocalRepository>

  
    
    
    
    
    <pluginGroups> pluginGroups>
  
  <proxies>
    
      <nonProxyHosts>local.net|some.host.comnonProxyHosts>
    proxy>
    -->
  proxies>

  
  <servers>
  <server>
    
    
      <id>db-releasesid>
       
      <username>rootusername>
       
      <password>root123password>
    server>
  servers>

  
  <mirrors>
	<mirror>
      <id>nexus-aliyunid>
      
      <mirrorOf>*mirrorOf>
       
      <name>Nexus aliyunname>
        
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
	mirror>
    <
            <mirrorOf>repositoryIdmirrorOf>
           
            <name>Human Readable Name for this Mirror.name>
          <url>http://196.160.1.211:8081/nexus/content/groups/public/url>
  mirrors>

  
  <profiles>
    
	<profile>   
	
     <id>db-profileid>  
      
     <activation>  
          <activeByDefault>trueactiveByDefault>  
      activation>  
      
	  <properties>  
		  <altReleaseDeploymentRepository>db-releases::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjot
altReleaseDeploymentRepository>

<altSnapshotDeploymentRepository>db-snapshot::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjotaltSnapshotDeploymentRepository>
	  properties>  

 	<repositories>
        <repository>
          <id>db-releasesid>
          <name>Repository for dbname>
          <url>db-releases::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjot4url>
          <releases>
	<enabled>trueenabled>
releases>

     <snapshots><enabled>falseenabled>
snapshots>
        repository>

 <repository>
          <id>db-snapshotsid>
          <name>Repository for dbname>
          <url>db-snapshots::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjot4url>
          <releases>
	<enabled>falseenabled>
releases>

     <snapshots><enabled>trueenabled>
snapshots>
        repository>
      repositories>
    profile>
   
  profiles>

  
  <activeProfiles>
    <activeProfile>db-profileactiveProfile>
  activeProfiles>
 
settings>

2.mirror和repository的关系

  • mirror 相等于一个拦截器,会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。

例如, 有一个项目,我们可能会在公司和家都进行编码,并在项目pom.xml配置了A Maven库。在公司带宽比较大网速快,访问A库很快,所以maven管理依赖和插件都从A库下载;在家用的是家庭宽带,带宽有限网速比较慢,访问A库很慢,但是访问B库很快。这时,只要在家的setting.xml里配置如下

<mirrors>
    <mirror>
        <id>Bid>
        <name>B Centralname>
        <url>http://www.B.com/maven2url>
        <mirrorOf>AmirrorOf>
    mirror>
mirrors>

让B库成为A 库的mirror,项目中配置的还是A库,就可不用更改项目pom.xml里对于A库的相关配置,就可以让项目去B库下载依赖。

3.repository和servers的关系

  • servers中配置的认证信息,是用户访问远程repository的认证信息,如果不配置就会报401,提示没有权限访问对应的远程仓库

  • 在配置代理仓库(其他企业的私服)时我们在profile-repositories中添加repository同时也要在servers中添加一个server,不然也是没有权限的

4.activeProfiles

  • 表示被激活的profile,当同时配置多个时可以激活多个profile,根据profile定义的先后顺序来进行覆盖取值的,后定义的会覆盖前面定义的,此标签通常用来做个多环境配置切换,如果是同一个环境建议将所有的repository都放到同一个profile里面

二、项目打包到maven私有仓库的两种方式

  1. settings中配置要推送的远程仓库
 <properties>  
		  <altReleaseDeploymentRepository>db-releases::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjot
altReleaseDeploymentRepository>

<altSnapshotDeploymentRepository>db-snapshot::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjotaltSnapshotDeploymentRepository>
	  properties>  

2.settings和pom.xml配合使用
注:此中方法在settings文件中不需要配置properties内容了

  • 在settings中配置签名认证
<server>
      <id>db-releasesid>
      <username>rootusername>
      <password>root123password>
<id>db-snapshotid>
      <username>rootusername>
      <password>root123password>
 server>
  • 在项目的pom.xml中配置
<project>
	<distributionManagement>
	    <repository>
	        <id>db-releaseid>
	        <name>Release Repositoryname>
	        <url>db-releases::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjoturl>
	    repository>
	    <snapshotRepository>
	        <id>db-snapshotid>
	        <name>Snapshot Repositoryname>
	        <url>db-snapshot::default::https://packages.aliyun.com/maven/repository/230491-release-6sdsjoturl>
	    snapshotRepository>
	distributionManagement>
project>

你可能感兴趣的:(settings,mavne,maven,xml,java)