maven學習系列(五)- maven之setting.xml詳解

來源:互聯網
上載者:User
2015/11/16


topic :maven<基本命令,setting.xml,注意事項>


points: localResponsity,pluginGroups,server,mirror,profile,activeProfile


1 基本命令 
<1>mvn package,mvn install,mvn deploy的區別
mvn package 打包
mvn install 打包並安裝到本地庫
mvn deploy  打包並安裝到遠程庫


常用
mvn compile 編譯
mvn package Dmaven.test.skip=true 跳過測試編譯


<2>例子
eg1 產生war包並部署到tomcat中
mvn -Pdev clean install -Dmaven.test.skip=true
cd beauty-api-web/target/
rm -rf beauty-api-web/*
unzip beauty-api-*.war -d beauty-api-web
rm -rf /data/local/beauty-api/webapps/ROOT/*
cp -r beauty-api-web/* /data/local/beauty-api/webapps/ROOT/


eg2 產生war包,並copy到相應目錄
mvn clean install -Pdev -Dmaven.test.skip=true clean package
cd target
scp sogou-brandtask-*.jar root@*.*.*.*:/data/task/task_online/sogou-brandtask.jar


2 pom檔案和setting.xml檔案相關知識點
<1>localResponsity 本地倉庫 
eg:<localRepository>E:/maven/repository</localRepository>


<2>pluginGroups 外掛程式組,這個元素包含了一系列pluginGroup元素,每個又包含了一個groupId。
當一個plugin被使用,而它的groupId沒有被提供的時候,pluginGroups列表將被搜尋。
eg:<pluginGroups>
        <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
org.morbay.jetty:jetty-maven-plugin:run => mvn jetty:run

<3>mirrors 鏡像
eg: <mirrors>
         <mirror>
             <id>planetmirror.com</id>
             <name>PlanetMirror Australia</name>
             <url>http://downloads.planetmirror.com/pub/maven2</url>
             <mirrorOf>central</mirrorOf>
         </mirror>
     </mirrors>


<4>server 伺服器,倉庫所在伺服器的使用者名稱,密碼等相關資訊


<5>distributionManagement 分發器,包含多種倉庫,一般包含napShot和release倉庫


<6>profiles,包括repositories,pluginRespositories,properties,activation
<<1 activation 啟用,如果所有指定的條件都達到了,那麼,activation就被觸發,而且不需要一次性全部達到
  <profiles>
         <profile>
            <id>test</id>
            <activation>
                 <activeByDefault>false</activeByDefault>
                 <jdk>1.5</jdk>
                 <os>
                     <name>Windows XP</name>
                     <family>Windows</family>
                     <arch>x86</arch>
                     <version>5.1.2600</version>
                 </os>
                 <property>
                     <name>mavenVersion</name>
                     <value>2.0.3</value>
                 </property>
                 <file>
                     <exists>${basedir}/file2.properties</exists>
                     <missing>${basedir}/file1.properties</missing>
                 </file>
             </activation>
             ...
         </profile>
     </profiles>
當以上條件全部達到時,啟用test


<<2 properties 屬性
eg:<profiles>
         <profile>
             ...
             <properties>
                 <user.install>${user.home}/our-project</user.install>
             </properties>
             ...
         </profile>
     </profiles>
#如果這個profile被啟用,那麼屬性${user.install}就可以被訪問了,profiles中的profile
#需要啟用才能使用,可以通過activation 或者 -p 顯示的啟用 
 
<<3 repositories 倉庫是Maven用來構築構建系統的本地倉庫的遠程項目集合。它來自於被Maven叫做外掛程式和依賴的本地倉庫。
不同的遠程倉庫包含不同的項目,當profile被啟用,他們就會需找匹配的release或者snapshot構件。
eg:<profiles>
         <profile>
         ...
         <repositories>
             <repository>
                 <id>codehausSnapshots</id>
                 <name>Codehaus Snapshots</name>
                 <releases>
                     <enabled>false</enabled>
                     <updatePolicy>always</updatePolicy>
                     <checksumPolicy>warn</checksumPolicy>
                 </releases>
                 <snapshots>
                     <enabled>true</enabled>
                     <updatePolicy>never</updatePolicy>
                     <checksumPolicy>fail</checksumPolicy>
                 </snapshots>
                 <url>http://snapshots.maven.codehaus.org/maven2</url>
                 <layout>default</layout>
             </repository>
         </repositories>
         <pluginRepositories>
         ...
         </pluginRepositories>
         ...
         </profile>
     </profiles>
 
<<4 plugin repositorie外掛程式倉庫,倉庫包含了兩種重要類型的構件。第一種是用來做其他構件依賴的構件,這是在中央倉庫中的大多數外掛程式。
另外一種類型的構件就是外掛程式pluginRepository元素指向一個可以找到新外掛程式的遠程地址。
<<5 Active Profiles被啟用的profiles
eg:<activeProfiles>
       <activeProfile>env-test</activeProfile>
   </activeProfiles>


3 注意事項
<1>maven中snapShot和release的區別
<<1 maven中的倉庫分為兩種,snapshot快照倉庫和release發布倉庫。snapshot快照倉庫用於儲存開發過程中的不穩定版本,
release正式倉庫則是用來儲存穩定的發行版本。定義一個組件/模組為快照版本,只需要在pom檔案中在該模組的版本號碼後加上-SNAPSHOT即可(必須大寫)
<<2 快照版本mvn deploy時會自動發布到快照版本庫中,在不更改版本號碼的情況下,直接編譯打包時,
maven會自動從鏡像伺服器上下載最新的快照版本。如果是正式發布版本,那麼在mvn deploy時會自動發布到正式版本庫中,
而使用正式版本的模組,在不更改版本號碼的情況下,編譯打包時如果本地已經存在該版本的模組則不會主動去鏡像伺服器上下載


<2>settings.xml存在於兩個地方:
1.安裝的地方:$M2_HOME/conf/settings.xml
2.使用者的目錄:${user.home}/.m2/settings.xml
前者又被叫做全域配置,後者被稱為使用者配置。如果兩者都存在,它們的內容將被合并,並且使用者範圍的settings.xml優先


<3>當setting.xml中的profile被啟用時,會覆蓋pom中相同id的profile的配置資訊


<4>maven2中snapshot快照庫和release發布庫的應用
pom.xml
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>cc.mzone</groupId>
    <artifactId>myjar</artifactId>
    <version>${project.release.version}</version>
    <packaging>jar</packaging>
 
    <distributionManagement>
        <repository>
   <id>mzone-release</id>
   <url>http://192.168.1.88/nexus/content/repositories/mzone-release</url>
        </repository>
        <snapshotRepository>
   <id>mzone-snapshot</id>
   <url>http://192.168.1.88/nexus/content/repositories/mzone-snapshot</url>
        </snapshotRepository>
    </distributionManagement>
 
    <properties>
        <project.release.version>0.1-SNAPSHOT</project.release.version>
    </properties>
 
    <profiles>
        <profile>
            <id>product</id>
   <properties>
         <project.release.version>0.1</project.release.version>
   </properties>
        </profile>
    </profiles>
</project>
setting.xml
<servers>
    <server>
        <id>mzone-release</id>
        <username>deployment</username>
        <password>deployment</password>
    </server>
    <server>
        <id>mzone-snapshot</id>
        <username>deployment</username>
        <password>deployment</password>
    </server>
</servers>
1、如果在發布時使用mvn deploy -P product的命令,那麼會自動使用0.1作為發布版本,那麼根據maven處理snapshot和release的規則,
   由於版本號碼後不帶-SNAPSHOT故當成是正式發布版本,會被發布到release倉庫;
2、如果發布時使用mvn deploy命令,那麼就會使用預設的版本號碼0.1-SNAPSHOT,此時maven會認為是快照版本,會自動發布到快照版本庫。
3、在distributionManagement段中配置的是snapshot快照庫和release發布庫的地址,採用nexus作為鏡像伺服器,伺服器的帳號和密碼存
   放在server中。
   
5 maven緩衝問題。
有時候由於maven項目無法更新,可以刪除.repository快取檔案





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.