我们知道了如何在POM中配置远程仓库,但考虑这样的情况:在一个公司内部,同时进行这3个项目,而且以后随着这几个项目的结束,越来越多的项目会开始;同时,公司内部建立一个Maven仓库。我们统一为所有这些项目配置该仓库,于是不得不为每个项目提供同样的配置。问题出现了,这是重复 !
其实我们可以做到只配置一次,在哪里配置呢?就是settings.xml。
不过事情没有那么简单,不是简单的将POM中的<repositories>及<pluginRepositories>元素复制到settings.xml中就可以,setting.xml不直接支持 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:
Xml代码- <settings>
- ...
- <profiles>
- <profile>
- <id>dev</id>
- <!-- repositories and pluginRepositories here-->
- </profile>
- </profiles>
- <activeProfiles>
- <activeProfile>dev</activeProfile>
- </activeProfiles>
- ...
- </settings>
- <settings>
- ...
- <profiles>
- <profile>
- <id>dev</id>
- <!-- repositories and pluginRepositories here-->
- </profile>
- </profiles>
- <activeProfiles>
- <activeProfile>dev</activeProfile>
- </activeProfiles>
- ...
- </settings>
这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用<activeProfiles>元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。
使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。
镜像
如果你的地理位置附近有一个速度更快的central镜像,或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。
以下的mirror配置用maven.net.cn覆盖了Maven自带的central:
Xml代码- <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>
- <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>
这里唯一需要解释的是<mirrorOf>,这里我们配置central的镜像,我们也可以配置一个所有仓库的镜像,以保证该镜像是Maven唯一使用的仓库:
Xml代码- <settings>
- ...
- <mirrors>
- <mirror>
- <id>my-org-repo</id>
- <name>Repository in My Orgnization</name>
- <url>http://192.168.1.100/maven2</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
- </mirrors>
- ...
- </settings>
- <settings>
- ...
- <mirrors>
- <mirror>
- <id>my-org-repo</id>
- <name>Repository in My Orgnization</name>
- <url>http://192.168.1.100/maven2</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
- </mirrors>
- ...
- </settings>
关于更加高级的镜像配置,可以参考:http://maven.apache.org/guides/mini/guide-mirror-settings.html。
分发构件至远程仓库
mvn install 会将项目生成的构件安装到本地Maven仓库,mvn deploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。
我们需要配置POM的distributionManagement来指定Maven分发构件的位置,如下:
Xml代码- <project>
- ...
- <distributionManagement>
- <repository>
- <id>nexus-releases</id>
- <name>Nexus Release Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
- </repository>
- <snapshotRepository>
- <id>nexus-snapshots</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
- ...
- </project>
- <project>
- ...
- <distributionManagement>
- <repository>
- <id>nexus-releases</id>
- <name>Nexus Release Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
- </repository>
- <snapshotRepository>
- <id>nexus-snapshots</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
- ...
- </project>
Maven区别对待release版本的构件和snapshot版本的构件,snapshot为开发过程中的版本,实时,但不稳定,release版本则比较稳定。Maven会根据你项目的版本来判断将构件分发到哪个仓库。
一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:
Xml代码- <settings>
- ...
- <servers>
- <server>
- <id>nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>nexus-snapshots</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
- ...
- </settings>
- <settings>
- ...
- <servers>
- <server>
- <id>nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>nexus-snapshots</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
- ...
- </settings>
需要注意的是,settings.xml中server元素下id的值必须与POM中repository或snapshotRepository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它人可见的,而settings.xml是本地的。
小结
本文介绍了Maven仓库,它是什么?本地仓库,远程仓库,中央仓库具体是指什么?并介绍了如何在POM中配置项目层次的仓库,在settings中配置用户层次的仓库,以及mirror。本文还介绍了如何安装构件到本地仓库,如何分发构件至仓库。