maven上手非常容易,是因为maven提供了很多默认配置,不过有时候,面对一些稍微复杂的情况,需要我们自己配置,我们就需要了解一点maven的原理。
例如,公司有一个私服,但是很多子公司在使用,每个公司的repository不一样,有时候有些jar只放在他们自己的repository中,所以需要访问他们的repository,同时也需要访问自己的repository,这时应该如何配置呢?
匹配到仓库之后,如果这个仓库如果有镜像(mirror),就会使用mirror配置的仓库地址。

maven的conf目录下settings.xml中localRepository配置
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\repositorylocalRepository>
settings>
maven的conf目录下settings.xml中profile配置,如果私服规划比较好,基本不会变,就可以使用全局配置,这样可以避免在每个项目中配置。
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>aliyun-repositoryid>
<repositories>
<repository>
<id>aliyunid>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
<updatePolicy>dailyupdatePolicy>
<checksumPolicy>ignorechecksumPolicy>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
profile>
<profile>
<id>owner-repositoryid>
<repositories>
<repository>
<id>ownerid>
<name>自己组织的代码库name>
<url>http://192.168.5.5:7777/nexus/content/groups/publicurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
profile>
<profile>
<id>other-repositoryid>
<repositories>
<repository>
<id>otherid>
<name>公司内部其他组织的仓库name>
<url>http://192.168.5.5:7777/nexus/content/groups/group-shareurl>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
profile>
profiles>
<activeProfiles>
<activeProfile>aliyun-repositoryactiveProfile>
<activeProfile>owner-repositoryactiveProfile>
<activeProfile>other-RepositoryactiveProfile>
activeProfiles>
settings>
多个激活的repository,会按顺序依次查找。
为什么有个默认的中央仓库central会生效,是因为Maven安装目录下/lib/maven-model-builder-${version}.jar中的org\apache\maven\model\pom-4.0.0.xml中配置了:
<repositories>
<repository>
<id>centralid>
<name>Central Repositoryname>
<url>https://repo.maven.apache.org/maven2url>
<layout>defaultlayout>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
项目配置在项目的pom.xml中,项目配置仓库基本都是用的是repositories
如果,项目要访问一些特殊的仓库,就需要单独配置项目仓库,例如,很多商用库就通过这种方式配置
<repositories>
<repository>
<id>maven-publicid>
<name>私服公用仓库地址name>
<url>http://192.168.5.5:7777/nexus/content/groups/publicurl>
repository>
<repository>
<id>maven-thirdpartyid>
<name>私服第三方包仓库地址name>
<url>http://192.168.5.5:7777/nexus/content/repositories/thirdparty/url>
repository>
repositories>
镜像配置:
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>aliyunid>
<name>阿里云仓库地址name>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
settings>
mirror是镜像,也意味着,只会查找第一个匹配到的mirror,查找顺序是按mirror的id字典序查找,而不是配置文件中的先后顺序(这操作比较迷,一是不直观,二是我为了顺序得去改id,相当于id有2个职责了)
只会找第一个匹配的mirror的前提是mirror可以访问,所以更准确的说是找第一个可以访问的mirror。
例如,central中央仓库配置了2个mirror,id分别是A和B,现在要找一个org.meeet.helper.jar包,如果A的url能ping通,就只会在这个url仓库查找,哪怕没有找到,都不会去B的url仓库找。只有当A的url不能ping通的时候,才会去B的url仓库查找。
mirrorOf匹配规则:
要分清mirror和repository,mirror只是repository的镜像,很多初学的朋友总想通过mirror来配置repository仓库,所以会出现根本不生效、或者一些奇怪复杂的配置。
所以,mirror尽量就只配置一个私服仓库,如公司的私服或者阿里云的仓库来作为central中央仓库的镜像,用于加快访问。
如果,需要访问多个仓库,尽量通过前面介绍的profile和repository来配置,如果自己的Nexus私服还可以通过配置group来处理。
Nexus是Maven仓库管理器,前面我们提到了私服(私有仓库),就可以使用Nexus,它能简化内部仓库的维护和外部仓库的访问。
例如,公司内部,我们就可以使用Nexus搭建私服,大家都配置私服地址,然后由Nexus去读取外部的库,当然也可以管理发布内部的库。
Nexus有4种仓库类型:

<distributionManagement>
<snapshotRepository>
<id>snapshotid>
<url>http://192.168.5.5:7777/repository/maven-snapshots/url>
snapshotRepository>
<repository>
<id>releaseid>
<url>http://192.168.5.5:7777/repository/maven-releases/url>
repository>
distributionManagement>
配置私服用户密码
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>releaseid>
<username>adminusername>
<password>123456password>
server>
<server>
<id>snapshotid>
<username>adminusername>
<password>123456password>
server>
servers>
settings>
打包插件:
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-release-pluginartifactId>
<version>2.5.3version>
<configuration>
<tagBase>${project.artifactId}-${project.version}tagBase>
<tagNameFormat>v@{project.version}tagNameFormat>
<autoVersionSubmodules>trueautoVersionSubmodules>
<generateReleasePoms>falsegenerateReleasePoms>
<arguments>-DskipTestsarguments>
<goals>deploygoals>
configuration>
plugin>