【maven3学习之七】maven中的仓库简介

我的感受仓库是maven的一大特点,使用仓库为咱们能够不用四处搜寻咱们须要的依赖,并且若是运用的好还会有其余意想不到的好处。通常来讲仍是要从概念开始。不过此次咱们不说概念了,仓库我的认为就是放咱们可能须要用到的依赖,插件或者项目输出的一个存放处。仓库主要有两类,本地仓库和远程仓库。还有一种特殊的远程仓库就是私服。apache

本地仓库服务器

  在以前例子中,maven学习之三中,配置的就是在本地d盘创建了一个本地库。一个构件可以被解析使用的前提是必须在本地库。本地库的配置方法以下网络

 1: <?xml version="1.0" encoding="UTF-8"?>
 2: 
 3: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
 4:           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5:           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 6:   <!-- localRepository
 7:    | The path to the local repository maven will use to store artifacts.
 8:    |
 9:    | Default: ~/.m2/repository
 10:   <localRepository>/path/to/local/repo</localRepository>
 11:   -->
 12:   <localRepository>D:\maven_localRepository</localRepository>
 13:   ...
 14: </settings>
经过设定localRepository标签来指定本地库的路径。
远程仓库,本地仓库一开始是没有的,它须要从远处仓库上下载须要的构件。例如咱们开始的时候须要从中央库中下载构件。
私服,私服是一个比较独特的远程仓库,他部署在本地局域网,用以免重复下载浪费带宽。特别是对于网络环境很差的状况下,使用私服确实是个不错的主意。
远程仓库的配置方法
 1: <project xmlns="http://maven.apache.org/POM/4.0.0"
 2:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3:   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 4:                       http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5:   ...
 6:   <repositories>
 7:     <repository>
 8:       <releases>
 9:         <enabled>false</enabled>
 10:         <updatePolicy>always</updatePolicy>
 11:         <checksumPolicy>warn</checksumPolicy>
 12:       </releases>
 13:       <snapshots>
 14:         <enabled>true</enabled>
 15:         <updatePolicy>never</updatePolicy>
 16:         <checksumPolicy>fail</checksumPolicy>
 17:       </snapshots>
 18:       <id>codehausSnapshots</id>
 19:       <name>Codehaus Snapshots</name>
 20:       <url>http://snapshots.maven.codehaus.org/maven2</url>
 21:       <layout>default</layout>
 22:     </repository>
 23:   </repositories>
 24:   <pluginRepositories>
 25:     ...
 26:   </pluginRepositories>
 27:   ...
 28: </project>
id是用来惟一标识这个远程仓库的,若是须要仓库须要验证登陆的话能够在setting.xml中对应这个id设定用户名和密码。
releases,snapshots:maven经过他们来控制发布版构件和快照版构件(也能够理解为测试版或者未完成版)的下载和更新。
enable:若是为ture表示开启对应构件类型的下载,若是false表示关闭下载。
updatePolicy:maven的更新策略,always表示老是,never表示从不。daily(默认)表示天天,interval:X表示X分钟。
checksumPolicy:maven的校验策略。maven发布到仓库也会发布相应的校验文件,当下载的时候会进行校验。你能够选择ignore,
fail,warn。
layout:仓库的布局,通常选择默认布局。
 
远程仓库认证的配置
 1: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 2:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3:   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
 4:                       http://maven.apache.org/xsd/settings-1.0.0.xsd">
 5:   ...
 6:   <servers>
 7:     <server>
 8:       <id>server001</id>
 9:       <username>my_login</username>
 10:       <password>my_password</password>
 11:       <privateKey>${user.home}/.ssh/id_dsa</privateKey>
 12:       <passphrase>some_passphrase</passphrase>
 13:       <filePermissions>664</filePermissions>
 14:       <directoryPermissions>775</directoryPermissions>
 15:       <configuration></configuration>
 16:     </server>
 17:   </servers>
 18:   ...
 19: </settings>
id:对应的是pom.xml中repository和mirror中的id,标识须要链接的服务器。
username,password:对应服务器须要的用户名和密码。
privateKey,passphrase:对应认真的私钥文件和字符串。使用私钥的时候要保证忽略的密码,由于若是有密码这个私钥会被忽略。
filePermissions,directoryPermissions:设定文件和目录的权限。
将项目部署到远程仓库
部署第三方构件是私服的一大特点,你能够将本身的项目发布到私服上供其余团队成员使用。
 
 1: <project xmlns="http://maven.apache.org/POM/4.0.0"
 2:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3:   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 4:                       http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5:   ...
 6:   <distributionManagement>
 7:     <repository>
 8:       <uniqueVersion>false</uniqueVersion>
 9:       <id>corp1</id>
 10:       <name>Corporate Repository</name>
 11:       <url>scp://repo/maven2</url>
 12:       <layout>default</layout>
 13:     </repository>
 14:     <snapshotRepository>
 15:       <uniqueVersion>true</uniqueVersion>
 16:       <id>propSnap</id>
 17:       <name>Propellors Snapshots</name>
 18:       <url>sftp://propellers.net/maven</url>
 19:       <layout>legacy</layout>
 20:     </snapshotRepository>
 21:     ...
 22:   </distributionManagement>
 23:   ...
 24: </project>
id,name:用以表示这个远程仓库。
uniqueVersion:设定为true的时候表示在发布时是否生成一个惟一的版本编号或者使用版本编号做为地址的一部分。
url:指定远程仓库的路径和访问协议。
layout:指定仓库的仓库的布局。
 
快照版本:
  在项目开发过程当中有可能一个模块依赖于另外一个模块,可是依赖的模块处于开发阶段,频繁变动版本,这样的状况如何处理比较方便呢,那就是使用快照的方式。例如在发行版2.1上进行开发,那咱们配置为2.1-SNAPSHOT,如何发布到私服上,在发布的过程当中,maven会自动为版本加上时间戳。另外一个依赖的想项目配置依赖的版本也是2.1-SNAPSHOT,那么maven会自动寻找最新的版本进行下载更新。
 
镜像:
  若是仓库A可以提供仓库B全部的文件,那么咱们就说A是B的镜像。
 
 1: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 2:   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3:   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
 4:                       http://maven.apache.org/xsd/settings-1.0.0.xsd">
 5:   ...
 6:   <mirrors>
 7:     <mirror>
 8:       <id>planetmirror.com</id>
 9:       <name>PlanetMirror Australia</name>
 10:       <url>http://downloads.planetmirror.com/pub/maven2</url>
 11:       <mirrorOf>central</mirrorOf>
 12:     </mirror>
 13:   </mirrors>
 14:   ...
 15: </settings>
id,name:用于描述镜像。
url:镜像的路径。
mirrorof:central表示中央库的意思。
配置私服镜像
 1: <settings>
 2:   ...
 3:   <mirrors>
 4:     <mirror>
 5:       <id>internal-repository</id>
 6:       <name>Maven Repository Manager running on repo.mycompany.com</name>
 7:       <url>http://repo.mycompany.com/proxy</url>
 8:       <mirrorOf>external:*,!foo</mirrorOf>
 9:     </mirror>
 10:     <mirror>
 11:       <id>foo-repository</id>
 12:       <name>Foo</name>
 13:       <url>http://repo.mycompany.com/foo</url>
 14:       <mirrorOf>foo</mirrorOf>
 15:     </mirror>
 16:   </mirrors>
 17:   ...
 18: </settings>
mirrorof:*表示匹配全部库;external:*表示匹配全部远程仓库;repo1,repo2表示匹配这两个仓库;*,!repo1表示匹配全部处理repo1.
相关文章
相关标签/搜索