1. 程式人生 > >maven中全域性配置檔案settings.xml及專案pom.xml的詳解3

maven中全域性配置檔案settings.xml及專案pom.xml的詳解3

(2)   properties
作用:對應profile的擴充套件屬性列表。
maven屬性和ant中的屬性一樣,可以用來存放一些值。這些值可以在pom.xml中的任何地方使用標記${X}來使用,這裡X是指屬性的名稱。屬性有五種不同的形式,並且都能在settings.xml檔案中訪問。

<!-- 
  1. env.X: 在一個變數前加上"env."的字首,會返回一個shell環境變數。例如,"env.PATH"指代了$path環境變數(在Windows上是%PATH%)。 
  2. project.x:指代了POM中對應的元素值。例如: <project><version>1.0
</version></project>通過${project.version}獲得version的值。 3. settings.x: 指代了settings.xml中對應元素的值。例如:<settings><offline>false</offline></settings>通過 ${settings.offline}獲得offline的值。 4. Java System Properties: 所有可通過java.lang.System.getProperties()訪問的屬性都能在POM中使用該形式訪問,例如 ${java.home
}。 5. x: 在<properties/>元素中,或者外部檔案中設定,以${someVar}的形式使用。 --> <properties> <user.install>${user.home}/our-project</user.install> </properties>

 
 
(3) Repositories(重點)
作用:遠端倉庫列表,它是maven用來填充構建系統本地倉庫所使用的一組遠端倉庫。

<repositories>
  <!--包含需要連線到遠端倉庫的資訊 -->
<repository> <!--遠端倉庫唯一標識 --> <id>codehausSnapshots</id> <!--遠端倉庫名稱 --> <name>Codehaus Snapshots</name> <!--如何處理遠端倉庫裡釋出版本的下載 --> <releases> <!--true或者false表示該倉庫是否為下載某種型別構件(釋出版,快照版)開啟。 --> <enabled>false</enabled> <!--該元素指定更新發生的頻率。Maven會比較本地POM和遠端POM的時間戳。這裡的選項是:always(一直),daily(預設,每日),interval:X(這裡X是以分鐘為單位的時間間隔),或者never(從不)。 --> <updatePolicy>always</updatePolicy> <!--當Maven驗證構件校驗檔案失敗時該怎麼做-ignore(忽略),fail(失敗),或者warn(警告)。 --> <checksumPolicy>warn</checksumPolicy> </releases> <!--如何處理遠端倉庫裡快照版本的下載。有了releases和snapshots這兩組配置,POM就可以在每個單獨的倉庫中,為每種型別的構件採取不同的策略。例如,可能有人會決定只為開發目的開啟對快照版本下載的支援。參見repositories/repository/releases元素 --> <snapshots> <enabled /> <updatePolicy /> <checksumPolicy /> </snapshots> <!--遠端倉庫URL,按protocol://hostname/path形式 --> <url>http://snapshots.maven.codehaus.org/maven2</url> <!--用於定位和排序構件的倉庫佈局型別-可以是default(預設)或者legacy(遺留)。Maven 2為其倉庫提供了一個預設的佈局;然而,Maven 1.x有一種不同的佈局。我們可以使用該元素指定佈局是default(預設)還是legacy(遺留)。 --> <layout>default</layout> </repository> </repositories>

 
 
(4) pluginRepositories
作用:發現外掛的遠端倉庫列表。
和repository類似,只是repository是管理jar包依賴的倉庫,pluginRepositories則是管理外掛的倉庫。
maven外掛是一種特殊型別的構件。由於這個原因,外掛倉庫獨立於其它倉庫。pluginRepositories元素的結構和repositories元素的結構類似。每個pluginRepository元素指定一個Maven可以用來尋找新外掛的遠端地址。

<pluginRepositories>
  <!-- 包含需要連線到遠端外掛倉庫的資訊.參見profiles/profile/repositories/repository元素的說明 -->
  <pluginRepository>
    <releases>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </releases>
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </snapshots>
    <id />
    <name />
    <url />
    <layout />
  </pluginRepository>
</pluginRepositories>

10、ActiveProfiles
作用:手動啟用profiles的列表,按照profile被應用的順序定義activeProfile。
該元素包含了一組activeProfile元素,每個activeProfile都含有一個profile id。任何在activeProfile中定義的profile id,不論環境設定如何,其對應的 profile都會被啟用。如果沒有匹配的profile,則什麼都不會發生。
例如,env-test是一個activeProfile,則在pom.xml(或者profile.xml)中對應id的profile會被啟用。如果執行過程中找不到這樣一個profile,Maven則會像往常一樣執行。

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <activeProfiles>
    <!-- 要啟用的profile id -->
    <activeProfile>env-test</activeProfile>
  </activeProfiles>
  ...
</settings>

  到這裡settings.xml的配置方法己寫完,pom.xml中的配置方法和settings.xml差不多,這裡不做累贅。
 
 
  其中第9中的 profiles 配置詳細說明了profiles內部的具體配置,分成了4小項說明,請大家仔細參考。