[轉]maven2使用手冊

maven2 起步
    相信maven1 大家都已經很熟悉了,具體maven能做什麼,就不詳細說了。個人覺得maven在開源項目中用的還是比較多的,公司內部,就不太清楚了。我以前的公司用過一段時間,不過後來就沒有下文了。
    與maven1 相比,maven2可算是幾乎重寫了,不過從速度來說應該更快。
    主要的幾個新特性包括:(詳細參考http://www.ibm.com/developerworks/cn/opensource/os-maven2/index.html
    1. 更快、更簡單
        速度方面可以比上ant了
    2. 更少的配置文件
        現在的配置文件只剩下了settings.xml和pom.xml了。
    3.  Plugin語言更換
        語言開始支持java,BeanShell和ant
    4.  提供了預定義的模版
        這點是最有幫助的,用戶可以自己定義自己的項目模版了,就像用appfuse一樣生成項目結構
    5. 生命週期的引入
         在Maven2中有了明確的生命週期概念,而且都提供與之對應的命令,使得項目構建更加清晰明瞭。
    6.  新增Dependency Scope   
         這點也比較重要,有些用於test範圍的包,可以不用加入依賴了
    7.  傳遞依賴,簡化依賴管理
        這是最爲方便的,可以省了很多配置。如a 依賴 b,b 依賴c  默認 a也會依賴 c。但是也會帶來隱患,如版本衝突。不過maven 也已經考慮到了,可以使用exclusions來排除相應的重複依賴
介紹了那麼多,現在切入正題,開始maven2 之旅:
首些下載需要的工具:
maven2: http://maven.apache.org/download.html 最主要的
maven-proxy:用來代理repository,使用本地庫代替maven2的遠程庫
            http://maven-proxy.codehaus.org/
continuum:一個不錯的持續整合工具,用於自動build。支持ant,maven
http://maven.apache.org/continuum/
svn:版本控制工具相信都已經配置了。
maven 用於eclipse的插件 ,在maven主站有下載,不錯的插件。當然idea也有相應的插件
最後,http,服務器是必不可少的。用於內部開發使用。
可以使用apache ,或者jetty  http://www.mortbay.org/
安裝:
    安裝maven2很簡單,把下載來的maven包解開就行了。(目前我的配置都在win2003上,還沒有應用於linux,所有所有的配置都針對windows).增加相應的環境變量m2_home=maven2的安裝目錄,不要忘了設置java_home的目錄。另外在path中增加%m2_home%\bin;可以直接在命令行下面使用mvn。
其他工具的安裝在後續的文章會介紹。
開始第一個mvean2項目:
  mvn archetype:create -DgroupId=com.mycompany.app \
                   -DartifactId=my-app
  簡單介紹一下 groupId相當於你的組織,如同org.springframework,會轉化爲相應得本地路徑 artifactId,你主要的jar包名稱,也就是你要打成的jar 名稱。
編譯應用資源
    mvn complile
        編譯相應的jave 文件
編譯測試類以及運行測試類
    mvn test
       運行測試類
如果只想編譯test,執行
    mvn test-compile
打包和安裝你的本地庫
打包:
    mvn package
安裝:
    mvn install
創建web site
    mvn site
清除所有輸出
    mvn clean
創建相關的ide文件
    mvn idea:idea        或者    mvn eclipse:eclipse
    順便說一下,maven2 是有生命週期這一概念的,也就是說如果你執行package,相應的以前步驟,如compile,test等都會自動執行。
剛開始執行會比較慢,需要從maven2遠程庫中下載所有的文件到本地。如果你的本地沒有相應的依賴包,則每次maven都會去遠程下載,所以配置一個鏡像庫就比較重要了。
    另外介紹一下主要的參考資料:
    mavn2 主站:主要的pom和settings.xml參考資料
    Better Builds with Maven     http://www.mergere.com/m2book_download.jsp
    不錯的書,主要通過例子介紹。可惜都是E文的,花點時間還是值得的。
    下一個主題,會說一下maven2的主要配置。
--------------------------------------------------------------------------------

 
maven 配置篇 之 settings.xml
    maven2 比起maven1 來說,需要配置的文件少多了,主要集中在pom.xml和settings.xml中。
    先來說說settings.xml,settings.xml對於maven來說相當於全局性的配置,用於所有的項目。在maven2中存在兩個settings.xml,一個位於maven2的安裝目錄conf下面,作爲全局性配置。對於團隊設置,保持一致的定義是關鍵,所以maven2/conf下面的settings.xml就作爲團隊共同的配置文件。保證所有的團隊成員都擁有相同的配置。當然對於每個成員,都需要特殊的自定義設置,如用戶信息,所以另外一個settings.xml就作爲本地配置。默認的位置爲:${user.dir}/.m2/settings.xml目錄中(${user.dir} 指windows 中的用戶目錄)。
    settings.xml基本結構如下:
    <settings xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                               http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>
簡單介紹一下幾個主要的配置因素:
localRepository:表示本地庫的保存位置,也就是maven2主要的jar保存位置,默認在${user.dir}/.m2/repository,如果需要另外設置,就換成其他的路徑。
offline:如果不想每次編譯,都去查找遠程中心庫,那就設置爲true。當然前提是你已經下載了必須的依賴包。
Servers
   在POM中的 distributionManagement元素定義了開發庫。然而,特定的username和pwd不能使用於pom.xml,所以通過此配置來保存server信息
  <servers>
    <server>
      <id>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>

id:server 的id,用於匹配distributionManagement庫id,比較重要。
username, password:用於登陸此服務器的用戶名和密碼
privateKey, passphrase:設置private key,以及passphrase
filePermissions, directoryPermissions:當庫文件或者目錄創建後,需要使用權限進行訪問。參照unix文件許可,如664和775
Mirrors
表示鏡像庫,指定庫的鏡像,用於增加其他庫
  <mirrors>
    <mirror>
      <id>planetmirror.com</id>
      <name>PlanetMirror Australia</name>
      <url>http://downloads.planetmirror.com/pub/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

id,name:唯一的標誌,用於區別鏡像
url:鏡像的url
mirrorOf:此鏡像指向的服務id
Proxies
此設置,主要用於無法直接訪問中心的庫用戶配置。
  <proxies>
    <proxy>
      <id>myproxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>

id:代理的標誌
active:是否激活代理
protocol, host, port:protocol://host:port 代理
username, password:用戶名和密碼
nonProxyHosts: 不需要代理的host
Profiles
  類似於pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
  剛開始接觸的時候,可能會比較迷惑,其實這是maven2中比較強大的功能。從字面上來說,就是個性配置。
  單獨定義profile後,並不會生效,需要通過滿足條件來激活。
 repositories 和pluginRepositories
 定義其他開發庫和插件開發庫。對於團隊來說,肯定有自己的開發庫。可以通過此配置來定義。
 如下的配置,定義了本地開發庫,用於release 發佈。
    <repositories>
        <repository>
          <id>repo-local</id>
       <name>Internal 開發庫</name>
       <url>http://192.168.0.2:8082/repo-local</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
    <pluginRepository>
    <id>repo-local</id>
    <name>Internal 開發庫</name>
    <url>http://192.168.0.2:8082/repo-local</url>
    <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
    </releases>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    <layout>default</layout>
    </pluginRepository>
    </pluginRepositories>
releases, snapshots:每個產品的版本的Release或者snapshot(注:release和snapshot的區別,release一般是比較穩定的版本,而snapshot基本上不穩定,只是作爲快照)
properties
  maven 的properties作爲placeholder值,如ant的properties。
包括以下的5種類型值:

env.X,返回當前的環境變量
project.x:返回pom中定義的元素值,如project.version
settings.x:返回settings.xml中定義的元素
java 系統屬性:所有經過java.lang.System.getProperties()返回的值
x:用戶自己設定的值
Activation
  用於激活此profile
  <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>

jdk:如果匹配指定的jdk版本,將會激活
os:操作系統
property:如果maven能檢測到相應的屬性
file: 用於判斷文件是否存在或者不存在
除了使用activation來激活profile,同樣可以通過activeProfiles來激活
Active Profiles
表示激活的profile,通過profile id來指定。
  <activeProfiles>
    <activeProfile>env-test</activeProfile> 指定的profile id
  </activeProfiles>

 

--------------------------------------------------------------------------------
maven 配置篇 之pom.xml
    說完了settings.xml配置,下來說一下maven2的主要配置pom.xml
什麼是pom?
    pom作爲項目對象模型。通過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。
快速察看:
<project>
  <modelVersion>4.0.0</modelVersion>
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>
基本內容:
    POM包括了所有的項目信息。
maven 相關:
pom定義了最小的maven2元素,允許groupId,artifactId,version。所有需要的元素

groupId:項目或者組織的唯一標誌,並且配置時生成的路徑也是由此生成,如org.codehaus.mojo生成的相對路徑爲:/org/codehaus/mojo
artifactId: 項目的通用名稱
version:項目的版本
packaging: 打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
classifier: 分類
POM關係:
主要爲依賴,繼承,合成
  依賴關係:
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
  </dependencies>

groupId, artifactId, version:描述了依賴的項目唯一標誌
可以通過以下方式進行安裝:
使用以下的命令安裝:
mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
創建自己的庫,並配置,使用deploy:deploy-file
設置此依賴範圍爲system,定義一個系統路徑。不提倡。
type:相應的依賴產品包形式,如jar,war
scope:用於限制相應的依賴範圍,包括以下的幾種變量:
compile :默認範圍,用於編譯
provided:類似於編譯,但支持你期待jdk或者容器提供,類似於classpath
runtime:在執行時,需要使用
test:用於test任務時使用
system:需要外在提供相應得元素。通過systemPath來取得
systemPath: 僅用於範圍爲system。提供相應的路徑
optional: 標註可選,當項目自身也是依賴時。用於連續依賴時使用
   獨佔性   
   外在告訴maven你只包括指定的項目,不包括相關的依賴。此因素主要用於解決版本衝突問題
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-embedder</artifactId>
      <version>2.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
表示項目maven-embedder需要項目maven-core,但我們不想引用maven-core
繼承關係
    另一個強大的變化,maven帶來的是項目繼承。主要的設置:
定義父項目
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>
</project>
    packaging 類型,需要pom用於parent和合成多個項目。我們需要增加相應的值給父pom,用於子項目繼承。主要的元素如下:

依賴型
開發者和合作者
插件列表
報表列表
插件執行使用相應的匹配ids
插件配置
子項目配置
<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>
  <artifactId>my-project</artifactId>
</project>
relativePath可以不需要,但是用於指明parent的目錄,用於快速查詢。
dependencyManagement:
用於父項目配置共同的依賴關係,主要配置依賴包相同因素,如版本,scope。
合成(或者多個模塊)
    一個項目有多個模塊,也叫做多重模塊,或者合成項目。
如下的定義:
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <modules>
    <module>my-project1<module>
    <module>my-project2<module>
  </modules>
</project>
build 設置
    主要用於編譯設置,包括兩個主要的元素,build和report
  build
    主要分爲兩部分,基本元素和擴展元素集合
注意:包括項目build和profile build
<project>
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>
  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>
基本元素
<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter1.properties</filter>
  </filters>
  ...
</build>
defaultGoal: 定義默認的目標或者階段。如install
directory: 編譯輸出的目錄
finalName: 生成最後的文件的樣式
filter: 定義過濾,用於替換相應的屬性文件,使用maven定義的屬性。設置所有placehold的值
資源(resources)
    你項目中需要指定的資源。如spring配置文件,log4j.properties
<project>
  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>
      ...
    </testResources>
    ...
  </build>
</project>
resources: resource的列表,用於包括所有的資源
targetPath: 指定目標路徑,用於放置資源,用於build
filtering: 是否替換資源中的屬性placehold
directory: 資源所在的位置
includes: 樣式,包括那些資源
excludes: 排除的資源
testResources: 測試資源列表
插件
  在build時,執行的插件,比較有用的部分,如使用jdk 5.0編譯等等
<project>
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.0</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>
    </plugins>
  </build>
</project>

extensions: true or false,是否裝載插件擴展。默認false
inherited: true or false,是否此插件配置將會應用於poms,那些繼承於此的項目
configuration: 指定插件配置
dependencies: 插件需要依賴的包
executions: 用於配置execution目標,一個插件可以有多個目標。
如下:
    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>echodir</id>
            <goals>
              <goal>run</goal>
            </goals>
            <phase>verify</phase>
            <inherited>false</inherited>
            <configuration>
              <tasks>
                <echo>Build Dir: ${project.build.directory}</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
  說明:

id:規定execution 的唯一標誌
goals: 表示目標
phase: 表示階段,目標將會在什麼階段執行
inherited: 和上面的元素一樣,設置false maven將會拒絕執行繼承給子插件
configuration: 表示此執行的配置屬性
插件管理
    pluginManagement:插件管理以同樣的方式包括插件元素,用於在特定的項目中配置。所有繼承於此項目的子項目都能使用。主要定義插件的共同元素
擴展元素集合
主要包括以下的元素:
Directories
用於設置各種目錄結構,如下:
  <build>
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
    ...
  </build>
Extensions
表示需要擴展的插件,必須包括進相應的build路徑。
<project>
  <build>
    ...
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ftp</artifactId>
        <version>1.0-alpha-3</version>
      </extension>
    </extensions>
    ...
  </build>
</project>
Reporting
    用於在site階段輸出報表。特定的maven 插件能輸出相應的定製和配置報表。
  <reporting>
    <plugins>
      <plugin>
        <outputDirectory>${basedir}/target/site</outputDirectory>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <reportSets>
          <reportSet></reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
Report Sets
    用於配置不同的目標,應用於不同的報表
<reporting>
    <plugins>
      <plugin>
        ...
        <reportSets>
          <reportSet>
            <id>sunlink</id>
            <reports>
              <report>javadoc</report>
            </reports>
            <inherited>true</inherited>
            <configuration>
              <links>
                <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
              </links>
            </configuration>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
更多的項目信息
name:項目除了artifactId外,可以定義多個名稱
description: 項目描述
url: 項目url
inceptionYear:創始年份
Licenses
<licenses>
  <license>
    <name>Apache 2</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
  </license>
</licenses>
Organization
配置組織信息
  <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
  </organization>
Developers
配置開發者信息
<developers>
    <developer>
      <id>eric</id>
      <name>Eric</name>
      <email>[email protected]</email>
      <url>http://eric.propellors.net</url>
      <organization>Codehaus</organization>
      <organizationUrl>http://mojo.codehaus.org</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://tinyurl.com/prv4t</picUrl>
      </properties>
    </developer>
  </developers>
Contributors
  <contributors>
    <contributor>
      <name>Noelle</name>
      <email>[email protected]</email>
      <url>http://noellemarie.com</url>
      <organization>Noelle Marie</organization>
      <organizationUrl>http://noellemarie.com</organizationUrl>
      <roles>
        <role>tester</role>
      </roles>
      <timezone>-5</timezone>
      <properties>
        <gtalk>[email protected]</gtalk>
      </properties>
    </contributor>
  </contributors>
環境設置
Issue Management
    定義相關的bug跟蹤系統,如bugzilla,testtrack,clearQuest等
  <issueManagement>
    <system>Bugzilla</system>
    <url>http://127.0.0.1/bugzilla</url>
  </issueManagement>
Continuous Integration Management
連續整合管理,基於triggers或者timings
  <ciManagement>
    <system>continuum</system>
    <url>http://127.0.0.1:8080/continuum</url>
    <notifiers>
      <notifier>
        <type>mail</type>
        <sendOnError>true</sendOnError>
        <sendOnFailure>true</sendOnFailure>
        <sendOnSuccess>false</sendOnSuccess>
        <sendOnWarning>false</sendOnWarning>
        <configuration><address>[email protected]</address></configuration>
      </notifier>
    </notifiers>
  </ciManagement>
Mailing Lists
  <mailingLists>
    <mailingList>
      <name>User List</name>
      <subscribe>[email protected]</subscribe>
      <unsubscribe>[email protected]</unsubscribe>
      <post>[email protected]</post>
      <archive>http://127.0.0.1/user/</archive>
      <otherArchives>
        <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
      </otherArchives>
    </mailingList>
  </mailingLists>
SCM
  軟件配置管理,如cvs 和svn
  <scm>
    <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
    <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
    <tag>HEAD</tag>
    <url>http://127.0.0.1/websvn/my-project</url>
  </scm>
Repositories
配置同setting.xml中的開發庫
Plugin Repositories
配置同 repositories
Distribution Management
用於配置分發管理,配置相應的產品發佈信息,主要用於發佈,在執行mvn deploy後表示要發佈的位置
1 配置到文件系統
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>file://${basedir}/target/deploy</url>
</repository>
</distributionManagement>
2 使用ssh2配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scp://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
3 使用sftp配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>sftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
4 使用外在的ssh配置
    編譯擴展用於指定使用wagon外在ssh提供,用於提供你的文件到相應的遠程服務器。
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scpexe://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>
5 使用ftp配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>ftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>
repository 對應於你的開發庫,用戶信息通過settings.xml中的server取得
Profiles
類似於settings.xml中的profiles,增加了幾個元素,如下的樣式:
  <profiles>
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>

 

--------------------------------------------------------------------------------
使用maven2 進行團隊配置
    對於團隊來說,建立統一的開發環境是必須的,而maven能很好幫助建立統一的環境。下面就介紹如何更有效的進行統一的配置。
準備工作:
   下載必須的軟件:
maven2: http://maven.apache.org/download.html 最主要的
maven-proxy:用來代理repository,使用代理來訪問多個遠程庫
            http://maven-proxy.codehaus.org/
continuum:一個不錯的持續整合工具,用於自動build。支持ant,maven
http://maven.apache.org/continuum/
svn:版本控制工具
創建一致的開發環境
  
    在共享的開發環境中,更好的建議是保持maven的兩個不同的配置文件分別管理,包括共享和用戶自定義設置。共同的配置包括在安裝目錄中,而單獨的開發設置保存在用戶本地目錄。
   
    全局的配置文件settings.xml

 
<servers>
       //公司內部庫,所有的release版本,serverid對應於repository id,用於在deploy時,訪問使用,主要保存用戶名和密碼
<server>
<id>internal</id>
<username>${website.username}</username>
<password>${website.pwd}</password>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
//目前的開發庫,用於snapshot庫
<server>
<id>snapshot</id>
<username>${website.username}</username>
<password>${website.pwd}</password>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
</servers>
<profiles>
<!--定義核心庫 maven 鏡像,由maven-proxy實現-->
<profile>
<id>central-repo</id>
<repositories>
<repository>
<id>central</id>
<name>Internal Repository</name>
<url>http://192.168.0.2:9999/repository</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Internal Repository</name>
<url>http://192.168.0.2:9999/repository</url>
</pluginRepository>
</pluginRepositories>
</profile>
<!--定義內部庫,包括公司的所有release版本-->
<profile>
<id>internal-repo</id>
<repositories>
<repository>
<id>internal</id>
<name>Internal Repository</name>
<url>http://192.168.0.2:8080/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>internal</id>
<name>Internal Plugin Repository</name>
<url>http://192.168.0.2:8080/repo-local</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
<!--定義內部開發庫 ,也可以合併snapshot和release-->
<profile>
<id>snapshot-repo</id>
<repositories>
<repository>
<id>snapshot</id>
<name>Internal Repository</name>
<url>http://192.168.0.2:8080/repo-snapshot</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:60</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>snapshot</id>
<name>Internal Plugin Repository</name>
<url>http://192.168.0.2:8080/repo-snapshot</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:60</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活相應得配置-->
<activeProfiles>
<activeProfile>central-repo</activeProfile>
<activeProfile>internal-repo</activeProfile>
<activeProfile>snapshot-repo</activeProfile>
</activeProfiles>
<!-- 插件默認groupId -->
<pluginGroups>
<pluginGroup>com.mycompany.plugins</pluginGroup>
</pluginGroups>
 
包括了以下的共享因素:
服務器設置典型是共同的,只有用戶名需要在用戶環境中設置。使用一致的定義來配置共同的設置
profile定義了共同的因素,內部開發庫,包括指定的組織或者部門發佈的產品。這些庫獨立於核心開發庫。
激活的profiles列表,用於激活相應的profile
plugin 組只有當你的組織中有自己定義的插件,用於命令行運行在pom中定義。
對於單獨的用戶來說,設置如下:

 
<settings>
<profiles>
<profile>
<id>property-overrides</id>
<properties>
<website.username>myuser</website.username>
<website.pwd>test</website.username>
</properties>
</profile>
</profiles>
</settings>

 

創建共享開發庫
    大多數組織將會創建自己的內部開發庫,用於配置,而中心開發庫用於連接maven
    設置內部開發庫是簡單的,使用http協議,可以使用存在的http 服務器。或者創建新的服務,使用apache,或者jetty
    假設服務器地址192.168.0.2 ,端口8080
    http://192.168.0.2:8080/repo-local
    設置另外一個開發庫,用於設置項目的snapshot庫http://192.168.0.2:8080/repo-snapshot
    中心鏡像庫,使用maven-proxy創建,當然也可以創建自己的鏡像。用於下載本地庫中沒有的artifact

maven-proxy 設置
    從網上直接下載maven-proxy-standalone-0.2-app.jar和 proxy.properties
    在命令行中,直接運行java -jar maven-proxy-standalone-0.2-app.jar  proxy.properties
主要的配置:
設置repo.list 中增加相應的庫就可以,如下定義:
repo.list=repo1.maven.org,...
#maven 的中心庫
repo.repo1.maven.org.url=http://repo1.maven.org/maven2
repo.repo1.maven.org.description=maven.org
repo.repo1.maven.org.proxy=one
repo.repo1.maven.org.hardfail=false
repo.repo1.maven.org.cache.period=360000
repo.repo1.maven.org.cache.failures=true
以後所有的遠程庫,都通過此方式增加。順便說一下,不要忘了註釋原來的example,那是沒有辦法訪問的。
其他配置如
端口號 port=9999
保存的位置 repo.local.store=target/repo
serverName=http://localhost:9999
創建標準的組織pom
定義共同的內容,包括公司的結構,如組織,部門以及團隊。
察看一下maven 的自身,可以作爲很好的參考。
如scm
 

<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven-parent</artifactId>
<version>1</version>
</parent>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm</artifactId>
<url>http://maven.apache.org/maven-scm/</url>
...
<modules>
<module>maven-scm-api</module>
<module>maven-scm-providers</module>
...
</modules>
</project>    

 

在maven父項目中可以看到如下定義:

 
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>1</version>
</parent>
<groupId>org.apache.maven</groupId>
<artifactId>maven-parent</artifactId>
<version>5</version>
<url>http://maven.apache.org/</url>
...
<mailingLists>
<mailingList>
<name>Maven Announcements List</name>
<post>[email protected]</post>
...
</mailingList>
</mailingLists>
<developers>
<developer>
...
</developer>
</developers>
</project>    
 
maven 父pom包括了共享的元素,如聲明郵件列表,開發者。並且大多數項目繼承apache組織:

 
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>1</version>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org/</url>
</organization>
<url>http://www.apache.org/</url>
...
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>http://svn.apache.org/maven-snapshot-repository</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
...
<distributionManagement>
<repository>
...
</repository>
<snapshotRepository>
...
</snapshotRepository>
</distributionManagement>
</project>    
 
對於項目自身來說,父pom很少更新。所以,最後的方式保存父pom文件在單獨的版本控制區域,它們能夠check out,更改和配置.
使用Continuum持久整合
    持續整合自動build你的項目,通過一定的時間,包括所有的衝突在早期察覺,而不是發佈的時候。另外持續整合也是一種很好的開發方式,使團隊成員能產生細微的,交互的變動,能更有效的支持平行開發進程。
    可以使用maven的continuum作爲持久整合的服務。
    安裝continuum,比較簡,使用以下的命令:
    C:\mvnbook\continuum-1.0.3> bin\win32\run
    可以通過http://localhost:8082/continuum來驗證
    爲了支持continuum 發送e-mail提醒,你需要相應的smtp服務用於發送信息。默認使用localhost:25,如果你沒有設置,編輯上面的文件改變smtp-host設置。
    下一步,設置svn目錄:
    svn co file://localhost/C:/mvnbook/svn/proficio/trunk proficio
    編輯pom.xml用於正確相應得e-mail地址。

 
...
<ciManagement>
<system>continuum</system>
<url>http://localhost:8080/continuum
<notifiers>
<notifier>
<type>mail</type>
<configuration>
<address>[email protected]</address>
</configuration>
</notifier>
</notifiers>
</ciManagement>
...
<scm>
<connection>
scm:svn:file://localhost/c:/mvnbook/svn/proficio/trunk
</connection>
<developerConnection>
scm:svn:file://localhost/c:/mvnbook/svn/proficio/trunk
</developerConnection>
</scm>
...
<distributionManagement>
<site>
<id>website</id>
<url>
file://localhost/c:/mvnbook/repository/sites/proficio
/reference/${project.version}
</url>
</site>
</distributionManagement>   
 
提交相應的pom,然後執行mvn install
如果你返回http://localhost:8082/continuum,你會看到相應的項目列表。
一旦你登錄後,你可以選擇mavan 2.0項目用於增加相應的項目。你可以增加你的url或者提交你的本地內容。
你可以使用本地pom url,如下file://localhost/c:mvnbook/proficio/pom.xml
在提交了此url後,continuum將會返回相應的成功信息。
以下的原則用於更好的幫助持續整合:
早提交,經常提交:當用戶經常提交時,持續整合是最有效的。這並不意味着,提交不正確的代碼。
經常運行build:用於最快檢測失敗
儘快修正失敗:當失敗發生時,應該馬上修正失敗
建議一個有效的版本
運行clean build
運行復雜的綜合測試
build所有的項目結構分支
持續運行項目的拷貝

 

 原文地址 http://www.duduwolf.com/wiki/2007/308.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章