使用
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
準 備 工作:
下 載 必 須 的 軟 件:
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>
// 公司內部 庫 ,所有的 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>
<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>
<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 所有的 項 目 結 構分支
持 續 運行 項 目的拷 貝