Maven仓库(四)

jopen 8年前

在了解Maven如何使用仓库后,我们将能更高效的使用Maven。

前面我们已经讲过Maven的坐标机制,任何Maven项目使用任何构件的方式都是完全相同的。在此基础上,Maven可以在某个位置统一存储所有Maven项目共享的构件,这个统一的位置就是仓库。实际的Maven项目将不再各自存储其依赖文件,它们只需要声明这些依赖的坐标,在需要的时候,Maven会自动根据坐标找到仓库中的构件,并使用它们。

1. Maven仓库布局

Maven是如何根据坐标来定位路径的呢?下面举一个实际的例子:
有这样一个构件:groupId=org.testng、artifactId=testng、version=5.8、classifier=jdk15、packaging=jar,其对应的路径生成步骤如下:

1)基于groupId,将org.testng转换成org/testng/。
2)基于artifactId,路径变成org/testng/testng/。
3)使用版本信息,得到路径为org/testng/testng/5.8/。
4)依次加上artifactId,分隔符连字号,以及version,于是构件路径变成了org/testng/testng/5.8/testng-5.8。
5)如果有classifier信息,路径就变成org/testng/testng/5.8/testng-5.8-jdk5。
最后由packaging决定构件的扩展名,所以最后的路径为org/testng/testng/5.8/testng-5.8-jdk5.jar。

2. Maven仓库的分类

仓库分类:本地仓库和远程仓库。Maven根据坐标寻找构件的时候,它先会查看本地仓库,如果本地仓库存在构件,则直接使用;如果没有,则从远程仓库查找,找到后,下载到本地。

1)本地仓库
默认情况下,每个用户在自己的用户目录下都有一个路径名为.m2/repository/的仓库目录。我们也可以自定义本地仓库的地址,如下:

这样,用户的本地仓库被设置成了D:/library/maven/repository/。我们需要从Maven安装目录复制$MAVEN_HOME/conf/settings.xml文件复制到D:/library/maven下进行编辑。大家不要直接修改全局目录下的settings.xml。

我们可以将依赖从远程仓库下载到本地仓库,也可以将本地仓库安装到Maven仓库中。如对前面我们讲过的hello-world项目执行mvn clean install(Eclipse中命令为clean install),结果如下:

[INFO] Scanning for projects...  [INFO]                                                                           [INFO] ------------------------------------------------------------------------  [INFO] Building hello-world 0.0.1-SNAPSHOT  [INFO] ------------------------------------------------------------------------  [INFO]   [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---  [INFO] Deleting D:\code\maven\hello-world\target  [INFO]   [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---  [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  [INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources  [INFO]   [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---  [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  [INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes  [INFO]   [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---  [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  [INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources  [INFO]   [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---  [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  [INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes  [INFO]   [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---  [INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports    -------------------------------------------------------   T E S T S  -------------------------------------------------------  Running com.alvinliang.maven.HelloWorldTest  Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.661 sec    Results :    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0    [INFO]   [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---  [INFO] Building jar: D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar  [INFO]   [INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---  [INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar  [INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom  [INFO] ------------------------------------------------------------------------  [INFO] BUILD SUCCESS  [INFO] ------------------------------------------------------------------------  [INFO] Total time: 13.245s  [INFO] Finished at: Thu Nov 28 22:22:25 CST 2013  [INFO] Final Memory: 10M/55M  [INFO] ------------------------------------------------------------------------
从上述输出,我们可以看到构件的hello-world-0.0.1-SNAPSHOT.jar,安装到了本地仓库D:\library\maven\repository下。


2)远程仓库
本地仓库好比书房,而远程仓库就像是书店。对于Maven来说,每个用户只有一个本地仓库,但是可以配置多个远程仓库。

a.中央仓库
Maven必须要知道至少一个可用的远程仓库,中央仓库就是这样一个默认的远程仓库,Maven的安装文件自带了中央仓库的配置。在settings文件有如下配置:

中央仓库包含了这个世界的绝大多数流行的开源Java构件,源码等。

b)私服
私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库,供局域网内的Maven用户使用。当Maven需要下载构件的时候,它先从私服请求,如果私服上没有构件,测从外部的远程仓库下载,存在私服上,再为Maven用户下载。现在可以使用最流行的Maven私服软件--Nexus。后面专门介绍如何搭建私服。

3. 远程仓库的配置

见如下代码清单:

<repositories>     <repository>      <id>central</id>      <name>Central Repository</name>      <url>https://nexus/sourcesense.com/nexus/content/repositories/public/</url>      <layout>default</layout>      <snapshots>       <enabled>false</enabled>      </snapshots>     </repository>  <repositories>

注意上面的id设置为central,使用nexus官方的仓库地址来代替中央仓库,因为有时候中央仓库很慢。上述配置中设置snapshots为false表示关闭central仓库对快照版本的下载支持。

4. 远程仓库的认证

在局部文件settings.xml中,在servers元素下配置,如下:

<servers>      <server>        <id>central</id>        <username>lb</username>        <password>123456</password>      </server>  <servers>

上述配置中id为关键元素,表示对那个远程仓库进行认证,这里配置的是中央仓库central。其中认证的用户名为lb,密码为123456。

5. 镜像

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。也就是说,任何一个可以从仓库Y获得的构件,都可以从它的镜像中获取。例如:http://maven.net.cn/content/groups/public/是重要仓库http://repo1.maven.org/maven2/在中国的镜像。由于地理位置的原因,镜像玩玩能够提供比中央仓库更快的服务。下面就是本人使用Nexus镜像来代替中央仓库的配置,编辑settings.xml,如下:

<mirrors>          <mirror>        <id>central</id>        <mirrorOf>*</mirrorOf>        <name>Human Readable Name for this Mirror.</name>        <url>http://localhost:8081/nexus/content/groups/public/</url>      </mirror>         <!-- mirror       | Specifies a repository mirror site to use instead of a given repository. The repository that       | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used       | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.       |      <mirror>        <id>mirrorId</id>        <mirrorOf>repositoryId</mirrorOf>        <name>Human Readable Name for this Mirror.</name>        <url>http://my.repository.com/repo/path</url>      </mirror>       -->    </mirrors>

上述配置中,mirrorOf指的是为那个仓库提供镜像,如果设置为central,表示为中央仓库的镜像,设置为*表示为任意仓库设置的镜像配置。

6. 仓库搜索服务

最后我们在为Maven编写依赖的时候,是不是不知道从何处开始,如何寻找到需要的依赖呢?下面我提供几个非常不错的网址:
http://mvnrepository.com/ 

https://repository.sonatype.org/

打开其中一个页面,直接搜索关键字,可以得到依赖配置,如下图:

来自: http://my.oschina.net/liangbo/blog/179936