【轉】maven詳解一

1. Maven生命週期

Maven的生命週期是對項目全部的構建過程進行抽象和統一。它包含了項目的清理、初始化、編譯、測試、打包、部署和站點生成等構建步驟。Maven的生命週期本質是定義項目構建的各個步驟,它自己是抽象的,並不做任何的具體工做,而是將構建過程當中的各個步驟任務交個相對應的插件來完成。這使得Maven的生命週期具備良好的擴展性,開發者能夠本身編寫插件實現代碼綁定到構建的某個步驟。固然,在絕大部分場景下,開發者沒必要這樣來作,由於Maven初始爲項目構建的各個步驟綁定了默認的插件。html

生命週期

Maven有三個相互獨立的生命週期。它們分別是:Clean Default Sitejava

生命週期 描述
Clean 用於清理項目
Default 用於構建項目
Site 用於生成項目站點

每一個獨立的生命週期都包含一些階段(phase),這些階段是一個有序的組合,後面的階段依賴於前面的階段。Maven主要的生命週期階段在下表中已用加粗字體標出。git

Clean生命週期
階段 描述
pre-clean 在實際項目清理以前執行所需的過程
clean 刪除上一個版本生成的全部文件
post-clean 執行完成項目清理所需的過程
Default生命週期
階段 描述
validate 驗證項目全部必要的信息都是可用的
initialize 初始化構建狀態,例如設置屬性或建立目錄
generate-sources 生成包含在編譯中的源代碼
process-sources 處理源代碼,過濾和替換一些變量的值
generate-resources 生成包含在包中的資源
process-resources 處理資源包,將其複製到目標目錄
compile 編譯項目的源代碼
process-classes 從編譯中對生成的文件進行後處理,如對Java類進行字節碼加強
generate-test-sources 生成包含在編譯中的測試源代碼
process-test-sources 處理源代碼,過濾和替換一些變量的值
generate-test-resources 生成包含在測試包中的資源
process-test-resources 處理資源包,將其複製到測試目標目錄
test-compile 編譯項目的測試源代碼
process-test-classes 從編譯中對生成的測試文件進行後處理,如對Java類進行字節碼加強
test 執行單元測試,測試代碼不會被打包部署
prepare-package 打包以前執行所需的任何操做
package 打包項目
pre-integration-test 集成測試前執行的操做,如設置環境等
integration-test 執行集成測試,若是須要,可將軟件包部署到可運行集成測試的環境
post-integration-test 集成測試後執行的操做,如清理環境
verify 檢查軟件包是否符合標準
install 將項目輸出構件安裝到本地倉庫
deploy 將項目輸出構件部署到遠程倉庫
Site生命週期
階段 描述
pre-site 在實際項目站點生成以前執行所需的過程
site 生成項目的站點文檔
post-site 執行完成站點生成所需的進程,並準備站點部署
site-deploy 將生成的站點文檔部署到指定的Web服務器
命令行命令

Maven執行項目構建的過程實質上是調用Maven生命週期階段的過程。Maven主要的生命週期階段並很少,其命令行命令幾乎都是基於這些階段的簡單組合。並且Maven的大三生命週期是相互獨立的,每一個生命週期的階段都有先後依賴的關係。任何一個簡單的或組合的命令,只有當前面的階段執行成功,後面的階段纔有機會得以繼續執行。若是前面的某個階段執行失敗,後面的階段就會被短路而致使沒法繼續執行下去。github

$ mvn clean deploy
  • clean:調用Clean生命週期的clean和clean以前的全部階段(即pre-clean和clean)
  • deploy:調用Default生命週期compile + test + package + install + deploy五個主要的階段

所以,執行該命令最終達到的效果是:
清理項目 → 編譯項目 → 單元測試 → 打包項目 → 將軟件包安裝到本地 → 將軟件包發佈到遠程倉庫express

生命週期內置綁定的插件

Maven的生命週期是抽象的,它自己不作任何具體的工做,項目構建的過程實質是調用Maven生命週期階段的過程,而Maven初始已經爲生命週期的大部分階段綁定了默認的插件,當外部調用Maven生命週期階段的時候,綁定到這些階段的插件就會來執行對應的具體工做。apache

Clean生命週期內置綁定的插件(詳見Lifecycles Referencebash

階段 插件
pre-clean  
clean maven-clean-plugin:2.5:clean
post-clean  

Default生命週期內置綁定的插件(詳見Plugin Bindings for default Lifecycle Reference服務器

階段 插件
process-sources maven-resources-plugin:2.6:resources
compile maven-compiler-plugin:3.1:compile
process-test-sources maven-resources-plugin:2.6:testResources
test-compile maven-compiler-plugin:3.1:testCompile
test maven-surefire-plugin:2.12.4:test
package maven-jar-plugin:2.4:jar
maven-ejb-plugin:2.3:ejb
maven-plugin-plugin:3.2:addPluginArtifactMetadata
maven-war-plugin:2.2:war
maven-ear-plugin:2.8:ear
maven-rar-plugin:2.2:rar
install maven-install-plugin:2.4:install
deploy maven-deploy-plugin:2.7:deploy

Site生命週期內置綁定的插件(詳見Lifecycles Reference網絡

階段 插件
pre-site  
site maven-site-plugin:3.3:site
post-site  
site-deploy maven-site-plugin:3.3:deploy

項目打包類型與生命週期內置綁定的插件

Maven項目的打包類型是由<packaging>xxoo</packaging>標籤元素的值來決定的。可選值常見的有jar war pom,以及不太經常使用的maven-plugin ejb ear rar等。
Maven的Default生命週期爲不一樣的打包類型內置綁定了不一樣的插件。而咱們知道,項目構建過程的具體工做是由插件來完成的,所以不一樣的打包類型調用相同的Maven生命週期階段,最終得以打出不一樣類型的軟件包。
app

<packaging>pom</packaging>類型打包方式與生命週期內置綁定的插件

<phases>
    <install>
        org.apache.maven.plugins:maven-install-plugin:2.4:install
    </install>
    <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
    </deploy>
</phases>

 

<packaging>jar</packaging>類型打包方式與生命週期內置綁定的插件

<phases>
    <process-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:resources
    </process-resources>
    <compile>
        org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
    </compile>
    <process-test-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
    </process-test-resources>
    <test-compile>
        org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
    </test-compile>
    <test>
        org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
    </test>
    <package>
        org.apache.maven.plugins:maven-jar-plugin:2.4:jar
    </package>
    <install>
        org.apache.maven.plugins:maven-install-plugin:2.4:install
    </install>
    <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
    </deploy>
</phases>

<packaging>war</packaging>類型打包方式與生命週期內置綁定的插件

<phases>
    <process-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:resources
    </process-resources>
    <compile>
        org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
    </compile>
    <process-test-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
    </process-test-resources>
    <test-compile>
        org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
    </test-compile>
    <test>
        org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
    </test>
    <package>
        org.apache.maven.plugins:maven-war-plugin:2.2:war
    </package>
    <install>
        org.apache.maven.plugins:maven-install-plugin:2.4:install
    </install>
    <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
    </deploy>
</phases>

 


2. Maven Super POM

全部maven模塊(項目)的POM(Project Object Model,項目對象模型)都隱式的繼承了Super POM。Super POM中定義了一組標準的配置變量。maven模塊(項目)的POM會自動繼承這些配置變量,你能夠在maven模塊(項目)的POM文件中直接使用它們,或者經過從新聲明以覆蓋它們。

假若Maven安裝的目錄用{MAVEN_HOME}來表示,那麼,Maven Super POM文件所在的路徑爲:{MAVEN_HOME}/lib/maven-model-builder-3.5.4.jar/org/apache/maven/model/pom-4.0.0.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

Super POM 的做用

  1. 定義了一個ID爲central的遠程倉庫(url即爲這個倉庫的地址),若是沒有其它特殊的聲明,maven模塊(項目)所須要的構件(jar包等)都會到這個遠程中央倉庫中去下載;

  2. 定義了一個插件倉庫,這個插件倉庫和上面定義的遠程倉庫用的是同一個倉庫,maven模塊(項目)所須要的插件包都會到這個遠程中央倉庫中去下載;

  3. 定義了一組標準的配置變量,在maven模塊(項目)的POM中能夠經過${project.build.XX}來使用它們,如 ${project.build.directory};

    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

     


3. Maven發佈構件到私服

Nexus3搭建的Maven私服爲例子,用管理員帳戶登陸系統平臺,並在管理控制檯的配置面板中建立一個用於發佈項目構件的帳戶(注:在Maven中,全部的依賴、插件、項目構建的輸出,均可以稱做是構件)。

建立角色

在Nexus3管理控制檯中,建立一個具備nx-component-uploadnx-repository-view-*-*-*兩個權限的角色:

建立用戶

在Nexus3管理控制檯中,建立一個用做構件部署的帳戶,並將剛剛建立的角色分配給該用戶:

settings.xml配置

在maven的用戶或全局settings.xml配置文件,添加以下代碼:

<servers>
    <server>
        <id>nexus-maven-snapshot</id>
        <username>deployment</username>
        <password>deployment123</password>
    </server>
    <server>
        <id>nexus-maven-releases</id>
        <username>deployment</username>
        <password>deployment123</password>
    </server>
</servers>

<server>用於配置鏈接到遠程服務器的帳戶名和帳戶密碼。主要用於發佈項目構件到Maven私服的時候,爲本地鏈接遠程私服服務器進行權限認證提供所需的用戶名和密碼。

pom.xml配置

在項目的pom.xml配置文件中,添加以下代碼:

<distributionManagement>
    <repository>
        <id>nexus-maven-releases</id>
        <url>http://10.10.10.121:8081/repository/maven-releases/</url>
    </repository>
</distributionManagement>

注:若是項目是快照版本(項目pom.xml配置文件中version的值含有*-SNAPSHOT)則使用<snapshotRepository>標籤替換<repository>標籤。

  •  <distributionManagement>用於配置項目構件發佈到maven私服的哪一個倉庫中存儲。
  •  <id>的值必須與settings.xml配置文件中<server>子標籤<id>的值匹配,不然在發佈構件到maven私服時,會因查找不到帳戶和密碼致使本地鏈接到遠程私服服務器失敗(maven構建的時候報401錯誤)。
  •  <url>用於指定當前項目構件發佈到maven私服時,具體上傳存儲到的那個倉庫的地址。

部署構件

在命令行中執行命令:

$ mvn clean deploy

若是部署的構件是一個快照版本,因爲存儲快照版本的倉庫是容許構件從新部署的,所以快照版本的構件在每次發佈時都會自動帶上一個時間戳標記,以做爲區分和歷史備份。如圖所示:


4. Maven倉庫

Maven倉庫分爲兩種,一種是本地倉庫,一種是遠程倉庫。
本地倉庫是maven用來在本地機器上存儲從遠程倉庫下載回來的構件的位置。
遠程倉庫包含了絕大多數流行的開源的java構件,項目依賴的構件通常均可以在這裏下載。不一樣的遠程倉庫可能包含不一樣的java構件。

本地倉庫和遠程倉庫的關係

遠程倉庫又分爲:中央倉庫(https://repo.maven.apache.org/maven2)、私服(如Nexus OSS)、其餘第三方公共庫(如阿里http://maven.aliyun.com/nexus/content/groups/public

大部分構件最初都是Maven從遠程倉庫下載回來存儲到本地倉庫的(也有一小部分是經過安裝命令直接安裝到本地倉庫的),一旦Maven從遠程倉庫下載一個構件回來並存儲到了本地倉庫,只要你不手工刪除該構件,同一個版本號的構件永遠不須要去遠程倉庫下載第二次。由於Maven在構建項目查找依賴的構件時都首先會在本地倉庫中去查找,若是在本地倉庫查找不到纔會去遠程倉庫中查找。一旦Maven在本地倉庫查找到了依賴所需的構件,就不會再去遠程倉庫中去查找和下載了。

Maven本地倉庫配置

在maven的用戶或全局的settings.xml配置文件,以下:

<settings>
    
    <localRepository>D:/application/repo/maven</localRepository>
    
</settings>

<localRepository>用於指定本地倉庫的位置。全部從遠程倉庫下載回來的構件都存儲在這個位置。

Maven遠程倉庫配置

方式一,在maven的用戶或全局的settings.xml配置文件中,找到<profiles>配置:

<profiles>
    <profile>
        ... ...
        <repositories>
            <repository>
                <id>nexus-maven-snapshots</id>
                <url>http://10.10.10.121:8081/repository/maven-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus-maven-snapshots</id>
                <url>http://10.10.10.121:8081/repository/maven-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
        ... ...
    </profile>
</profiles>

<repositories><pluginRepositories>在配置結構和做用上都類似,一個是用來配置遠程倉庫依賴類型的構件,另一個是插件類型的構件。<id>必須是惟一的。<url>是這個倉庫的地址。

方式二,在項目的pom.xml配置文件中,添加以下代碼:

<repositories>
    <repository>
        <id>fanlychie-maven-repo</id>
        <url>https://raw.github.com/fanlychie/maven-repo/releases</url>
    </repository>
</repositories>

配置結構和在settings.xml中的結構一致。其區別在於,一個是在maven配置文件中配置,一個是在項目pom文件中配置。若是是針對某個或幾個項目,可選擇方式二來配置。

Maven倉庫檢索順序

接下來作一個實驗,在maven的用戶或全局的settings.xml配置文件中,找到<profiles>配置:
而且在<repositories>標籤中配置多個倉庫,而且將倉庫的順序設置爲以下(用倉庫ID表示):
nexus-maven-snapshots → nexus-maven-releases → nexus-maven-3rd.party

<profiles>
    <profile>
        <id>jdk7-development</id>
        <activation>
            <jdk>1.7</jdk>
            <activeByDefault>true</activeByDefault>
        </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>
        <repositories>
            <repository>
                <id>nexus-maven-snapshots</id>
                <url>http://10.10.10.121:8081/repository/maven-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>nexus-maven-releases</id>
                <url>http://10.10.10.121:8081/repository/maven-releases/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>nexus-maven-3rd.party</id>
                <url>http://10.10.10.121:8081/repository/3rd.party/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus-maven-snapshots</id>
                <url>http://10.10.10.121:8081/repository/maven-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>nexus-maven-releases</id>
                <url>http://10.10.10.121:8081/repository/maven-releases/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>nexus-maven-3rd.party</id>
                <url>http://10.10.10.121:8081/repository/3rd.party/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

而後在項目的pom.xml配置文件中,添加以下代碼:

<repositories>
    <repository>
        <id>fanlychie-maven-repo</id>
        <url>https://raw.github.com/fanlychie/maven-repo/releases</url>
    </repository>
</repositories>

<dependencies>
    <!-- 該構件託管在fanlychie-maven-repo倉庫中 -->
    <dependency>
        <groupId>org.fanlychie</groupId>
        <artifactId>jexcel</artifactId>
        <version>2.2.2</version>
    </dependency>
</dependencies>

而後在命令行中執行命令:

$ mvn compile

命令執行時輸出的日誌信息:

能夠看到,maven在下載依賴所需的構件時檢索倉庫的順序依次爲(倉庫ID):nexus-maven-snapshots → nexus-maven-releases → nexus-maven-3rd.party → fanlychie-maven-repo → central
其本質的檢索順序是:本地倉庫 → maven用戶或全局配置文件settings.xml中的倉庫 → 項目pom.xml文件中配置的倉庫 → mavenSuper POM中配置的中央倉庫
maven在下載依賴所需的構件時,若是這個構件在本地倉庫找獲得,就不會再去遠程倉庫中查找。若是這個構件在本地倉庫中查找不到,就會按遠程倉庫配置時的順序,去各個倉庫裏面一個一個的檢索,只要這個構件在其中的任意一個倉庫中查找到了,就不會再繼續檢索其餘的倉庫了。

這是第二次構建項目時輸出的信息,能夠看到,在第一次構建成功後,再次構建項目的時候就不會再去遠程倉庫下載構件了。


5. Maven鏡像

若是一個倉庫X能夠提供倉庫Y存儲的全部構件,那麼就能夠稱倉庫X是倉庫Y的一個鏡像。因爲地理位置等緣由,國內網絡鏈接Maven官方的中央倉庫網速通常較慢或時常出現網絡不穩定的狀態,從而致使項目在構建所需的時間較長或失敗。使用鏡像的好處就是,它每每能提供比中央倉庫更快的服務(一般選擇地理位置上與本身較近且口碑較好的鏡像), 從而提升下載速度, 最終達到提升項目構建效率的目的。

在maven的用戶或全局的settings.xml配置文件中,找到在<mirrors>配置:

<mirrors>
    <mirror>
        <id>aliyun-maven-repo</id>
        <!-- 阿里雲公共鏡像 -->
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <!-- 匹配Maven中央倉庫, 該ID在超POM中定義的 -->
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

<mirror>用於配置一個鏡像。<id>必須是惟一的。<url>是鏡像的地址。<mirrorOf>用於匹配Maven倉庫(<repository>)的ID。常見的匹配規則有:

通配符 描述
* 匹配全部的Maven倉庫ID
repo1,repo2 只匹配ID爲repo1或repo2的倉庫
external:* 匹配除本地倉庫以外的全部倉庫
*,!repo1 匹配除ID爲repo1以外的全部倉庫

Maven在構建項目的時候,每當一個構件在本地倉庫中查找不到時,它就會去遠程中央倉庫(central,Super POM中聲明使用的遠程倉庫地址)中去下載。若是某個鏡像的<mirrorOf>匹配到該倉庫(如上面的aliyun-maven-repo鏡像),則去這個倉庫下載構件的請求就會被該鏡像攔截掉,並將這次請求轉發到該鏡像配置的地址中去下載。本地全部訪問遠程倉庫的請求都被轉換成對鏡像地址的請求,這就是鏡像的做用和意義。

相關文章
相關標籤/搜索