1. 介紹
Maven是Java開發者中流行的構建工具,Maven的好處之一是可以幫助減少構建應用程序時所依賴的軟件構件的副本,Maven建議的方法是將所有軟件構件存儲於一個叫做repository的遠程倉庫中。
Maven會從ibiblio.org中的公用倉庫中同步構件,這個公用倉庫下載緩慢、 不穩定,並且不包含一些構件的最新版本,而且不能上傳團隊私有的構件。通過設置內部Maven倉庫,團隊可以更有好地利用Maven倉庫的優勢並且克服使 用ibiblio上的倉庫時的缺點。
本文着眼於Maven倉庫工具應提供的一些功能,列出了選擇Maven倉庫工具的標準, 說明了使用Artifactory設置Maven倉庫的相關步驟。在Linux和Windows中設置Maven倉庫的過程是相同的,少數不同點在文中已 經用高亮顯示,本文列出了與這個Maven倉庫的使用有關的POM文件的範例,用截圖來向用戶闡述如何設置Maven倉庫,Maven和 artifactory配置範例也有列出。
1.1 背景知識
本文假定讀者熟悉以下的概念和技術:
Maven 2
J2EE web 服務器部署過程(e.g. 在Tomcat中部署Web應用程序)
XML
1.2 Maven倉庫的目的
Maven倉庫的目的是作爲團隊內所使用的所有軟件構件的內部私有構件倉庫,將 Maven構件(jar和pom)存儲到一個專門的Maven倉庫比將它們存儲到版本控制系統中更有優勢,這是因爲:
構件(jar)是二進制文件,不屬於版本控制系統,版本控制系統在處理文本文件方面比較好
保持較小的版本控制數據庫
Checkout、update和其他版本控制的操作可以更快
1.3 建立內部私有倉庫的優勢:
減少可能的版本衝突
減少首次構建時需要的手動干涉
中央倉庫包含所有依賴的軟件構件,引用單一的中央倉庫比引用多個獨立的本地庫要好
使用內部倉庫做clean構建時會快些,因爲Maven構件是從內部服務器而不是從因特網上的ibiblio服務器獲取。
1.4 Maven構件庫的類型
本地倉庫——位於開發者的機器,由開發者維護,它從位於‘~home/.m2’ 目錄的‘settings.xml’文件中定義的Maven倉庫上同步,如果在pom.xml或setting.xml文件中沒有設置內部私有遠程倉庫, 那麼開發者機器上的這個本地倉庫就會從ibiblio.org 上的公用Maven倉庫上同步。
內部私有遠程倉庫——這是我們將要設置的倉庫,我們將改變maven 的pom.xml或settings.xml文件來使用這個庫。
外部公用遠程倉庫——這是在ibiblio.org上的外部公用倉庫,默認情況下,Maven從這個倉庫上同步。
?
2.Maven倉庫概覽
2.1 沒有Maven倉庫下的開發環境
2.2 具有團隊內部遠程倉庫的開發環境
3. 本教程Maven倉庫的用例介紹
在內部倉庫中創建2個子庫
Ibiblio 緩存‘Ibiblio-cache’ ——用於存儲從ibiblio.org中下載的構件,可以公開訪問,它將從外部的ibiblio倉庫中同步
內部倉庫internal-maven-repository——用於存儲團隊內部的構件,它不與任何外部倉庫同步,是團隊專有的構件庫
可以創建另一個子倉庫,這是可選的,用於存儲那些公用的但是在ibiblio上暫時沒有的構件,例如一些構件的新最版本,這個倉庫不與內部倉庫同步。我們可以稱它爲第三方‘3rd-party’
瀏覽遠程庫,最好是通過一個web瀏覽器
在倉庫中搜索構件
從版本控制系統中下載代碼,修改settings.xml指向內部倉庫並且做一次沒有任何手工干涉的clean構建
向倉庫中安裝一個構件的新版本
向倉庫中大批導入構件
從倉庫中大批導出構件
手動備份倉庫,設置定時自動備份任務
4. Maven倉庫工具的選擇標準
理想的Maven倉庫工具應該:
? 開放源碼並且免費
? 提供管理工具
? 提供倉庫瀏覽器——最好是web瀏覽器而不是桌面應用程序
? 可以部署到標準web服務器中——例如Apache或Tomcat
? 創建、編輯、刪除子庫的能力
? 批量導入/導出的工具,用來向構件庫或從構件庫移動構件
? 訪問控制工具和匿名只讀訪問
? 安裝和使用簡單
? 備份功能
? 問題追蹤,論壇或其他獨立的信息來源
? 活躍的社區/開發者,這樣產品才能改進,bug才能修正
5. 不同Maven倉庫工具的比較
一些流行且免費的Maven倉庫工具有:
標準Maven代理codehaus—— http://maven-proxy.codehaus.org/
Dead simple Maven Proxy (DSMP) - http://www.pdark.de/dsmp/
Proximity - http://proximity.abstracthorizon.org/
Artifactory - http://www.jfrog.org/sites/artifactory/latest/
下面列出了一個比較:
codehaus | DSMP | Proximity | Artifactory | |
管理工具 | 否 | 基本 | 是 | 是 |
構件庫瀏覽器 | 基本 | 否 | 是 | 是 |
部署到標準Web服務器 | 是(不可配置) | 否 | 是 | 是(與Jetty綁定,支持Tomcat) |
子庫管理 | 是 | 是 | 是 | 是 |
批量導入/導出構件 | 否 | 否 | 否 | 是 |
安裝使用簡單 | 是 | 是(從代碼構建) | 是 | 是(AJAX界面) |
備份功能 | 否 | 否 | 否 | 是(使用quartz和cron表達式) |
問題追蹤 | JIRA,IRC | Wiki issues Tracker | Jira issues Tracker(良好的在線文檔) |
研究過以上所有產品之後得出結論,Artifactory 擁有我們要的所有標準。Proximity看樣子也有許多我們要的特性,我們將着眼於Artifactory 的使用。
關於Artifactory 的其他方面:
符合我們的需求
Apache 2.0 許可
運行於JDK 1.6和Tomcat 6。Artifactory以‘war’文件形式部署到Tomcat
使用Derby數據庫來存儲構件,數據以一個已發佈的知名的格式存儲
所有構件可以被批量導出到本地庫然後導入到其他庫,因而易於將構件從一個倉庫工具移到另一個,它也使得Maven庫的升級非常容易
兼容Java Content Repository 標準(JSR-170)。
使用Lucene作爲搜索引擎,倉庫可以被索引和重新索引
Ajax界面
易於定製界面
使用JIRA作爲問題追蹤系統
6. 設置Maven倉庫
6.1 軟件需求
Artifactory——從http://www.jfrog.org/sites/artifactory/latest/ 下載和安裝,當前最新版本是1.2.1-rc1,Artifactory是一個可以被安裝到Tomcat中去的Web應用程序
JDK 1.6 – 更多信息請參閱http://www.jfrog.org/sites/artifactory/latest/install.html
Tomcat 6
6.2 目錄結構
下載和解壓artifactory。目錄結構如下:
這些目錄是:
backup —— 倉庫的備份,可以使用cron表達式設置備份策略,quartz定時調度程序在指定的時間裏運行備份任務,備份時間間隔在 ‘ARTIFACTORY_INSTALLATION_FOLDER>/etc/artifactory.config.xml’文件中指定
bin ——內置jetty 服務器的運行批處理文件
data —— 包含derby數據庫文件,如果你想以一個空的構件庫開始,可以刪除這個目錄中的所有內容,在全新的artifactory 安裝中,這個目錄是空的。
etc —— artifactory 的配置文件,有‘artifactory.config.xml’ 、‘jetty.xml ‘和‘log4j.,properties’
lib —— 包含所有依賴的jar文件
logs —— 日誌文件
webapps —— 包含war文件,它可以被拷貝到Tomcat中安裝。
6.2 在Tomcat 6中部署
部署 ‘<ARTIFACTORY_INSTALLATION_FOLDER>/webapp’目錄中的war文件到 ‘<TOMCAT_INSTALLATION_FOLDER>/webapps’目錄中,在jdk1.6和Tomcat 6環境下不需要改變tomcat的配置,Tomcat 6會檢測並部署它。
當這個Web應用程序被部署的時候,需要這些信息:
保存構件的數據庫的位置
artifactory 的xml配置文件的位置
備份目錄的位置
可以在一個配置文件中指定所有3個信息,在Tomcat啓動期間我們只需要指定 artifactory的安裝目錄,artifactory會計算出其餘的信息。可選的方法是使用jdbc設置好derby數據庫的連接並且在Web應用 程序中配置好artifactory (通過在Web應用程序中包含artifactory.config.xml文件)。不管怎樣,這個方法是簡單的。
artifactory 安裝目錄的位置可以被設置爲一個環境變量,在Linux中,用‘.bash’來輸出artifactory 安裝目錄的位置的腳本是:
- export JAVA_OPTS = -D<SPAN class=hilite1>artifactory</SPAN>.home=/home/amangat/<SPAN class=hilite1>artifactory</SPAN>-1.2.1-rc1
export JAVA_OPTS = -Dartifactory.home=/home/amangat/artifactory-1.2.1-rc1
在Windows中,可以添加到Tomcat啓動選項中:
6.3 設置Maven倉庫
建議的方法是在我們的Maven倉庫中創建3個庫(或子庫),它們是:
內部私有倉庫private-internal-repository:這個倉庫包含僅在團隊內使用的構件,它們由開發者手動上傳,這個倉庫不與ibiblio 中的遠程倉庫同步,因爲這個倉庫(或子倉庫)中的構件是團隊私有的。
第三方庫3rd-party:這個倉庫包含公有的但不在ibiblio服務器上的構件,例如,最新版本的尚不在ibiblio的組件或jdbc驅動。這個倉庫不與ibiblio 同步,因爲ibiblio 沒有這些jar。
Ibiblio 緩存Ibiblio-cache:這個倉庫與ibiblio 倉庫同步,它是ibiblio 上的構件的緩存。
設置這三個倉庫,需要修改<ARTIFACTORY_INSTALLATION_FOLDER>/etc/artifactory.config.xml’中的配置:
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://<SPAN class=hilite1>artifactory</SPAN>.jfrog.org/xsd/1.0.0"
- xsi:schemaLocation="http://<SPAN class=hilite1>artifactory</SPAN>.jfrog.org/xsd/1.0.0
- http://www.jfrog.org/xsd/<SPAN class=hilite1>artifactory</SPAN>-v1_0_0.xsd">
- <!-- Backup every 12 hours -->
- <!--<backupCronExp>0 0 /12 * * ?</backupCronExp>-->
- <localRepositories>
- <localRepository>
- <key>private-internal-repository</key>
- <description>Private internal repository</description>
- <handleReleases>true</handleReleases>
- <handleSnapshots>true</handleSnapshots>
- </localRepository>
- <localRepository>
- <key>3rd-party</key>
- <description>3rd party jars added manually</description>
- <handleReleases>true</handleReleases>
- <handleSnapshots>false</handleSnapshots>
- </localRepository>
- </localRepositories>
- <remoteRepositories>
- <remoteRepository>
- <key>ibiblio</key>
- <handleReleases>true</handleReleases>
- <handleSnapshots>false</handleSnapshots>
- <excludesPattern>org/<SPAN class=hilite1>artifactory</SPAN>/**,org/jfrog/**</excludesPattern>
- <url>http://repo1.maven.org/maven2</url>
- </remoteRepository>
- </remoteRepositories>
- </config>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://artifactory.jfrog.org/xsd/1.0.0" xsi:schemaLocation="http://artifactory.jfrog.org/xsd/1.0.0 http://www.jfrog.org/xsd/artifactory-v1_0_0.xsd"> <!-- Backup every 12 hours --> <!--<backupCronExp>0 0 /12 * * ?</backupCronExp>--> <localRepositories> <localRepository> <key>private-internal-repository</key> <description>Private internal repository</description> <handleReleases>true</handleReleases> <handleSnapshots>true</handleSnapshots> </localRepository> <localRepository> <key>3rd-party</key> <description>3rd party jars added manually</description> <handleReleases>true</handleReleases> <handleSnapshots>false</handleSnapshots> </localRepository> </localRepositories> <remoteRepositories> <remoteRepository> <key>ibiblio</key> <handleReleases>true</handleReleases> <handleSnapshots>false</handleSnapshots> <excludesPattern>org/artifactory/**,org/jfrog/**</excludesPattern> <url>http://repo1.maven.org/maven2</url> </remoteRepository> </remoteRepositories></config>
啓動Tomcat並且輸入 http://localhost:8080/artifactor
下面是artifactory的首頁面:
以用戶名admin和密碼password登錄,點擊瀏覽倉庫的鏈接,你可以查看倉庫的內容。
7.配置Maven以使用新的倉庫
7.1 配置Maven的settings.xml 文件
Maven使用位於‘~/.m2/settings.xml’目錄的 settings.xml文件來獲取Maven倉庫 ,如果沒有在該文件中指定倉庫,Maven默認使用ibiblio.org上的倉庫, settings.xml文件必須被修改以使用新的倉庫:
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <tomcat5x.home>C:/InstalledPrograms/apache-tomcat-5.5.20</tomcat5x.home>
- </properties>
- <repositories>
- <repository>
- <id>central</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>snapshots</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>central</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- <pluginRepository>
- <id>snapshots</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
<profiles> <profile> <id>dev</id> <properties> <tomcat5x.home>C:/InstalledPrograms/apache-tomcat-5.5.20</tomcat5x.home> </properties> <repositories> <repository> <id>central</id> <url>http://localhost:8080/artifactory/repo</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>snapshots</id> <url>http://localhost:8080/artifactory/repo</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://localhost:8080/artifactory/repo</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>snapshots</id> <url>http://localhost:8080/artifactory/repo</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories> </profile></profiles>
7.2 使用pom.xml配置Maven
也可以在項目的pom.xml文件中設置倉庫,下面是pom.xml範例文件:
- <project 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
- <A href="http://maven.apache.org/maven-v4_0_0.xsd" target=_blank>http://maven.apache.org/maven-v4_0_0.xsd</A>">
- <modelVersion>4.0.0</modelVersion>
- <groupId>test</groupId>
- <artifactId>test</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>test</name>
- <url>http://maven.apache.org</url>
- <repositories>
- <repository>
- <id>central</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>snapshots</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>central</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- <pluginRepository>
- <id>snapshots</id>
- <url>http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/repo</url>
- <releases>
- <enabled>false</enabled>
- </releases>
- </pluginRepository>
- </pluginRepositories>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
<project 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.0http://maven.apache.org/maven-v4_0_0.xsd "><modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>test</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>test</name><url>http://maven.apache.org</url> <repositories> <repository> <id>central</id> <url>http://localhost:8080/artifactory/repo</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>snapshots</id> <url>http://localhost:8080/artifactory/repo</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://localhost:8080/artifactory/repo</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>snapshots</id> <url>http://localhost:8080/artifactory/repo</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories><dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency></dependencies></project>
7.3 使用Maven構件倉庫構建項目
在構建Maven項目的時候,所有的構件庫都會從新的倉庫下載,控件臺會顯示下載的過程:
用瀏覽器登錄到新的構件倉庫,你會發現artifactory 已經下載並緩存了ibiblio上的構件。
7.4 安裝構件到構件倉庫
可以通過Web界面或Maven命令行安裝構件,通過Web界面來安裝更簡單更快,並且不需要修改任何配置。通過命令行安裝需要修改settings.xml配置文件,可以在其他腳本中使用。
7.4.1 通過Web界面安裝構件
安裝步驟如下:
1. 上傳要部署的構件(‘jar’ 文件 或 ‘pom’ 文件)
2. 如果你上傳的是jar文件,Artifactory會爲它創建pom文件 。上傳的時候你可以指定上傳到哪個倉庫。
3. 上傳完後,上傳的構件與artifactory 自動創建的pom文件一起在倉庫中出現。
7.4.1 能過Maven命令行安裝構件
當用‘mvn clean install’ 命令的時候,Maven僅僅打包和安裝構件到本地倉庫,要把它安裝到APH內部倉庫中,我們必須在settings.xml中添加一條額外的配置:
- <settings>
- <servers>
- <server>
- <id>organisation-internal</id>
- <username>admin</username>
- <password>password</password>
- </server>
- </servers>
- </settings>
<settings> <servers> <server> <id>organisation-internal</id> <username>admin</username> <password>password</password> </server> </servers></settings>
安裝構件到內部Maven倉庫的命令是:
- mvn deploy:deploy-file -DrepositoryId=organisation-internal -Durl=http://localhost:8080/<SPAN class=hilite1>artifactory</SPAN>/private-internal-repository
- -DgroupId=test -DartifactId=test -Dversion=1.1 -Dpackaging=jar -Dfile=target/test-1.1.jar
mvn deploy:deploy-file -DrepositoryId=organisation-internal -Durl=http://localhost:8080/artifactory/private-internal-repository-DgroupId=test -DartifactId=test -Dversion=1.1 -Dpackaging=jar -Dfile=target/test-1.1.jar
repositoryId必須與settings.xml中定義的服務器ID相匹配,url必須包含構件將要被安裝到的倉庫的名稱。
新的構件會在倉庫中出現,並且artifactory 會自動爲我們創建pom文件。
8. Artifactory 的其他特性
8.1 備份構件倉庫
備份策略在 <ARTIFACTORY_INSTALLATION_FOLDER>/etc/artifactory.config.xml中指定,使用 cron表達式來指定定時備份任務,配置備份任務的元素在下面的代碼清單中用高亮顯示出來了
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://<SPAN class=hilite1>artifactory</SPAN>.jfrog.org/xsd/1.0.0"
- xsi:schemaLocation="http://<SPAN class=hilite1>artifactory</SPAN>.jfrog.org/xsd/1.0.0
- http://www.jfrog.org/xsd/<SPAN class=hilite1>artifactory</SPAN>-v1_0_0.xsd">
- <!-- Backup every 12 hours -->
- <backupCronExp>0 0 /12 * * ?</backupCronExp>
- <localRepositories>
- <localRepository>
- <key>private-internal-repository</key>
- <description>Private internal repository</description>
- <handleReleases>true</handleReleases>
- <handleSnapshots>true</handleSnapshots>
- </localRepository>
- <localRepository>
- <key>3rd-party</key>
- <description>3rd party jars added manually</description>
- <handleReleases>true</handleReleases>
- <handleSnapshots>false</handleSnapshots>
- </localRepository>
- </localRepositories>
- <remoteRepositories>
- <remoteRepository>
- <key>ibiblio</key>
- <handleReleases>true</handleReleases>
- <handleSnapshots>false</handleSnapshots>
- <excludesPattern>org/<SPAN class=hilite1>artifactory</SPAN>/**,org/jfrog/**</excludesPattern>
- <url>http://repo1.maven.org/maven2</url>
- </remoteRepository>
- </remoteRepositories>
- </config>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://artifactory.jfrog.org/xsd/1.0.0" xsi:schemaLocation="http://artifactory.jfrog.org/xsd/1.0.0 http://www.jfrog.org/xsd/artifactory-v1_0_0.xsd"> <!-- Backup every 12 hours --> <backupCronExp>0 0 /12 * * ?</backupCronExp> <localRepositories> <localRepository> <key>private-internal-repository</key> <description>Private internal repository</description> <handleReleases>true</handleReleases> <handleSnapshots>true</handleSnapshots> </localRepository> <localRepository> <key>3rd-party</key> <description>3rd party jars added manually</description> <handleReleases>true</handleReleases> <handleSnapshots>false</handleSnapshots> </localRepository> </localRepositories> <remoteRepositories> <remoteRepository> <key>ibiblio</key> <handleReleases>true</handleReleases> <handleSnapshots>false</handleSnapshots> <excludesPattern>org/artifactory/**,org/jfrog/**</excludesPattern> <url>http://repo1.maven.org/maven2</url> </remoteRepository> </remoteRepositories></config>
備份文件存儲在 ‘<ARTIFACTORY_INSTALLATION_FOLDER>/backups’目錄,備份文件的格式與開發者機器上的本地倉庫一致,這使得要移植倉庫內容到其他Maven倉庫工具中時非常容易。
8.2 其他特性
通過Web界面刪除構件
通過Web界面搜索構件
批量導入/導出倉庫中的所有構件
如果不要求使用Tomcat,可以使用內置的jetty服務器
9. 結論
內部私有Maven倉庫加快構建的過程並且使clean構建更簡單,它還幫助避免不同構件版本的衝突。
在這4款通用的Maven構件倉庫工具中,看樣子Artifactory 是比較好的產品。
Artifactory使得設置Maven倉庫變得簡單;它提供了一個好的Maven倉 庫工具應該實現的所有功能;團隊不會被鎖定在這個工具上,因爲可以容易地移植倉庫內容到其他倉庫工具上;對於不知道Maven倉庫如何工作的人來 說,Web界面使得他們能很容易地使用倉庫。