Maven
鏡像mirror
是爲了解決依賴下載的速度問題,倉庫repository
是使用指定的倉庫下載依賴。
maven
下載依賴的流程
以編譯工程下載依賴爲例:localRepository
本地倉庫-》nexus
私服(如果有)-》中央倉庫
具體如下:
maven
去settings
配置的localRepository
本地倉庫查找依賴- 本地倉庫沒有,根據
pom
文件或者settings
配置的repository
倉庫查找依賴 - 如果
settings
配置了mirror
,第二步的請求會被攔截,並使用mirror
配置的url
倉庫查找依賴,即mirrorOf
的配置會影響攔截的請求,一般情況下配置的是自己公司的私服
3.1.mirrorOf
配置*,無論pom
和settings
配置的repository
倉庫的url
是什麼,倉庫url
都會被替換成mirror
中配置的url
3.2.mirrorOf
配置業界公認可選值,此時mirror
的url
也應該是其匹配的倉庫地址
3.3.mirrorOf
自定義,一般情況下和pom
和settings
配置的repository
的id
保持一致,這樣鏡像會攔截匹配的倉庫的請求,不匹配的走中央倉庫;如果都不匹配,遍歷鏡像並下載依賴;部分匹配,攔截匹配並下載,其他不匹配的遍歷鏡像並下載。
3.4.其他配置,例如一個鏡像以逗號分隔,配置多個mirrorOf
,所有攔截的請求使用同一個倉庫
3.5.實際上mirrorOf
只是鏡像url
的一個別名,用來攔截倉庫請求並轉發到鏡像配置的url
中 - 如果鏡像中攔截所有請求到私服,但私服中沒有,私服會和中央倉庫打交道拉取依賴
mirrorOf
配置原則
- 自定義時,和
pom
和settings
配置的repository
的id
保持一致 - 如果所有的請求都攔截到自己的私服,那麼使用*
- 沒什麼原則,符合你的需求即可
配置案例
settings
的鏡像,id
和name
自定義,id
保證唯一,mirrorOf
配置*
攔截所有請求到私服
settings.xml
配置
<mirrors>
<mirror>
<id>nexus</id>
<name>nexus</name>
<mirrorOf>*</mirrorOf>
<url>http://192.168.1.11:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<!--激活配置 -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://192.168.1.11:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://192.168.1.11:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>env-dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
在settings
的profile
配置倉庫或者項目pom
文件中配置倉庫
pom.xml
中的配置
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://192.168.1.11:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus</name>
<url>http://192.168.1.11:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>