在POM中配置远程仓库
前面我们看到超级POM配置了ID为central的远程仓库,我们可以在POM中配置其它的远程仓库。这样做的原因有很多,比如你有一个局域网的远程仓库,使用该仓库能大大提高下载速度,继而提高构建速度,也有可能你依赖的一个jar在central中找不到,它只存在于某个特定的公共仓库,这样你也不得不添加那个远程仓库的配置。
这里我配置一个远程仓库指向中央仓库的中国镜像:
-
<project>
-
...
-
<repositories>
-
<repository>
-
<id>maven-net-cn </id>
-
<name>Maven China Mirror </name>
-
<url>http://maven.net.cn/content/groups/public/ </url>
-
<releases>
-
<enabled>true </enabled>
-
</releases>
-
<snapshots>
-
<enabled>false </enabled>
-
</snapshots>
-
</repository>
-
</repositories>
-
<pluginRepositories>
-
<pluginRepository>
-
<id>maven-net-cn </id>
-
<name>Maven China Mirror </name>
-
<url>http://maven.net.cn/content/groups/public/ </url>
-
<releases>
-
<enabled>true </enabled>
-
</releases>
-
<snapshots>
-
<enabled>false </enabled>
-
</snapshots>
-
</pluginRepository>
-
</pluginRepositories>
-
...
-
</project>
我们先看一下的配置,你可以在它下面添加多个,每个都有它唯一的ID,一个描述性的name,以及最重要的,远程仓库的url。此外,true告诉Maven可以从这个仓库下载releases版本的构件,而false告诉Maven不要从这个仓库下载snapshot版本的构件。禁止从公共仓库下载snapshot构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持。
关于的更详细的配置及相关解释,请参考:http://www.sonatype.com/books/maven-book/reference_zh/apas02s08.html。
至于,这是配置Maven从什么地方下载插件构件(Maven的所有实际行为都由其插件完成)。该元素的内部配置和完全一样,不再解释。
在settings.xml中配置远程仓库
我们知道了如何在POM中配置远程仓库,但考虑这样的情况:在一个公司内部,同时进行这3个项目,而且以后随着这几个项目的结束,越来越多的项目会开始;同时,公司内部建立一个Maven仓库。我们统一为所有这些项目配置该仓库,于是不得不为每个项目提供同样的配置。问题出现了,这是重复 !
其实我们可以做到只配置一次,在哪里配置呢?就是settings.xml。
不过事情没有那么简单,不是简单的将POM中的及元素复制到settings.xml中就可以,setting.xml不直接支持 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:
-
<settings>
-
...
-
<profiles>
-
<profile>
-
<id>dev </id>
-
-
</profile>
-
</profiles>
-
<activeProfiles>
-
<activeProfile>dev </activeProfile>
-
</activeProfiles>
-
...
-
</settings>
这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。
使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。
镜像
如果你的地理位置附近有一个速度更快的central镜像,或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。
以下的mirror配置用maven.net.cn覆盖了Maven自带的central:
- <settings>
...
<mirrors>
<mirror>
<id>maven-net-cn</id>
<name>Maven China Mirror</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
...
</settings>
核心配置(配置多个中央下载仓库中心)
-
<mirrors>
-
<!-- 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>
-
-->
-
<!--默认的中央仓库-->
-
<mirror>
-
<id>mirrorId </id>
-
<mirrorOf>repositoryId </mirrorOf>
-
<name>Human Readable Name for this Mirror. </name>
-
<url>http://my.repository.com/repo/path </url>
-
</mirror>
-
<!--自定义添加-->
-
<mirror>
-
<id>repo2 </id>
-
<mirrorOf>central </mirrorOf>
-
<name>Human Readable Name for this Mirror. </name>
-
<url>http://repo2.maven.org/maven2/ </url>
-
</mirror>
-
-
<mirror>
-
<id>ui </id>
-
<mirrorOf>central </mirrorOf>
-
<name>Human Readable Name for this Mirror. </name>
-
<url>http://uk.maven.org/maven2/ </url>
-
</mirror>
-
-
<mirror>
-
<id>ibiblio </id>
-
<mirrorOf>central </mirrorOf>
-
<name>Human Readable Name for this Mirror. </name>
-
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/ </url>
-
</mirror>
-
-
<mirror>
-
<id>jboss-public-repository-group </id>
-
<mirrorOf>central </mirrorOf>
-
<name>JBoss Public Repository Group </name>
-
<url>http://repository.jboss.org/nexus/content/groups/public </url>
-
</mirror>
-
<!--访问慢的网址放入到后面-->
-
<mirror>
-
<id>CN </id>
-
<name>OSChina Central </name>
-
<url>http://maven.oschina.net/content/groups/public/ </url>
-
<mirrorOf>central </mirrorOf>
-
</mirror>
-
<mirror>
-
<id>net-cn </id>
-
<mirrorOf>central </mirrorOf>
-
<name>Human Readable Name for this Mirror. </name>
-
<url>http://maven.net.cn/content/groups/public/ </url>
-
</mirror>
-
<mirror>
-
<id>JBossJBPM </id>
-
<mirrorOf>central </mirrorOf>
-
<name>JBossJBPM Repository </name>
-
<url>https://repository.jboss.org/nexus/content/repositories/releases/ </url>
-
</mirror>
-
</mirrors>
这里唯一需要解释的是,这里我们配置central的镜像,我们也可以配置一个所有仓库的镜像,以保证该镜像是Maven唯一使用的仓库:
- <</SPAN>settings>
- ...
- <</SPAN>mirrors>
- <</SPAN>mirror>
- <</SPAN>id>my-org-repo</</SPAN>id>
- <</SPAN>name>Repository in My Orgnization</</SPAN>name>
- <</SPAN>url>http://192.168.1.100/maven2</</SPAN>url>
- <</SPAN>mirrorOf>*</</SPAN>mirrorOf>
- </</SPAN>mirror>
- </</SPAN>mirrors>
- ...
- </</SPAN>settings>
关于更加高级的镜像配置,可以参考:http://maven.apache.org/guides/mini/guide-mirror-settings.html。
分发构件至远程仓库
mvn install 会将项目生成的构件安装到本地Maven仓库,mvndeploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。
我们需要配置POM的distributionManagement来指定Maven分发构件的位置,如下:
- <</SPAN>project>
- ...
- <</SPAN>distributionManagement>
- <</SPAN>repository>
- <</SPAN>id>nexus-releases</</SPAN>id>
- <</SPAN>name>Nexus Release Repository</</SPAN>name>
- <</SPAN>url>http://127.0.0.1:8080/nexus/content/repositories/releases/</</SPAN>url>
- </</SPAN>repository>
- <</SPAN>snapshotRepository>
- <</SPAN>id>nexus-snapshots</</SPAN>id>
- <</SPAN>name>Nexus Snapshot Repository</</SPAN>name>
- <</SPAN>url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</</SPAN>url>
- </</SPAN>snapshotRepository>
- </</SPAN>distributionManagement>
- ...
- </</SPAN>project>
Maven区别对待release版本的构件和snapshot版本的构件,snapshot为开发过程中的版本,实时,但不稳定,release版本则比较稳定。Maven会根据你项目的版本来判断将构件分发到哪个仓库。
一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:
- <</SPAN>settings>
- ...
- <</SPAN>servers>
- <</SPAN>server>
- <</SPAN>id>nexus-releases</</SPAN>id>
- <</SPAN>username>admin</</SPAN>username>
- <</SPAN>password>admin123</</SPAN>password>
- </</SPAN>server>
- <</SPAN>server>
- <</SPAN>id>nexus-snapshots</</SPAN>id>
- <</SPAN>username>admin</</SPAN>username>
- <</SPAN>password>admin123</</SPAN>password>
- </</SPAN>server>
- </</SPAN>servers>
- ...
- </</SPAN>settings>