Maven仓库

1. 仓库分类:

Maven仓库_第1张图片

仓库只分为两类:本地仓库和远程仓库。当maven根据坐标寻找构件的时候,它首先会查看本地仓库,如果本地仓库存在此构件,则直接使用;如果本地仓库不存在此构件,或者需要查看是否有更新的构件版本,maven就会去远程仓库查找,发现需要的构件之后,下载到本地仓库再使用。如果本地仓库和远程仓库都没有需要的构件,maven就会报错。

(1)本地仓库路径配置在setting.xml里面

<settings>
  <localRepository>D:\WorkSpace\repository</localRepository>
</settings>


(2)中央仓库为Maven自带的, Maven的安装文件自带了中央仓库的配置。可以打开jar文件../lib/maven-model-builder-*.*,*.jar,然后访问路径:org/apache/maven/model/pom-*.*.*.xml,可以看到如下的配置:

<repositories>  
    <repository>  
        <id>central</id>  
        <name>Maven Repository Switchboard</name>  
        <url>http://repo1.maven.org/maven</url>  
        <layout>default</layout>  
        <snapshots>  
            <enabled>false</enabled>  
        </snapshots>  
    </repository>  
</repositories> 
包含这段配置的文件是所有Maven项目都会继承的超级POM。这段配置使用id central对中央仓库进行唯一标识,其名称为Maven Repository Switchboard,它使用default仓库布局。最后需要注意的是snapshots元素,其子元素enabled的值为false,表示不从该中央仓库下载快照版本的构件



 (3)私服是自己在局域网内架设的远程仓库, 在开发阶段很节省带宽和时间。比如项目要开发了一个Dubbo接口, deploy(部署)一下到私服上, 其他项目组的同事就可以拉下来调用了。

Maven仓库_第2张图片

 

  - 在POM中配置自己远程仓库 (公司中很多人都会用到,一般不会这样配,采用下面一种全局方式)

  - 在setting.xml中配置

<settings>
...
<profiles>
    <profile>
      <id>devRemote</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>    
  </profiles>
  <activeProfiles>
    <activeProfile>devRemote</activeProfile>
  </activeProfiles>
...
</settings>
这里定义一个id为devRemote的profile,使用<activeProfiles>元素自动激活该profile。这样,就不用再为每个POM重复配置仓库,从而统一了开发远程仓库。



2. 通过Maven部署构件到远程仓库


(1) 在maven项目的pom里配置如下信息

<distributionManagement>
     <!-- 发布版本构建的仓库-->
    <repository>
        <id>nexus-releases</id>
        <url>http://ip:port/nexus/content/repositories/releases/</url>
    </repository>
  <!-- 快照版本仓库 -->
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://ip:port/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

(2) 部署构件到远程仓库

命令: mvn deploy(与install相反,install是下载到本地)

是snapshot就部署到snapshot地址, 否则就部署到发布版本地址。


(3) 在server.xml可配置带用户名和密码的认证远程仓库, 一般对内部使用的。分发构件到远程仓库需要认证,如果没有配置任何认证信息,往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:

 <servers>
    <server>
      <id>nexus-releases</id>
      <username>deployment</username>
      <password><![CDATA[deployment123]]></password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>deployment</username>
      <password><![CDATA[deployment123]]></password>
    </server>	
	
	<server>
      <id>thirdparty</id>
      <username>deployment</username>
      <password><![CDATA[deployment123]]></password>
    </server>	
	
    ...


  </servers>

需要注意的是,settings.xml中server元素下id的值必须与POM中repository或snapshotRepository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它人可见的,而settings.xml是本地的。



3. 镜像


如果地理位置附近有一个下载更快的镜像地址,或者想覆盖central仓库配置,或者想为所有POM指定唯一的一个远程仓库(一般公司都这么配,统一开发环境),可以使用settings.xml中的mirror配置。

<settings>
 ...
 <mirrors>
    <mirror>
      <id>dev</id>
      <mirrorOf>*</mirrorOf>
      <url>host:port/*/*...</url>
    </mirror>
	...
 </mirrors>
 ...
</settings>
<mirrorOf>*</mirrorOf>这样就覆盖了所有的其他的仓库使用, 这个仓库称为仓库的唯一代理。


你可能感兴趣的:(maven,snapshot,发布)