我的感受倉庫是maven的一大特點,使用倉庫爲咱們能夠不用四處搜尋咱們須要的依賴,並且若是運用的好還會有其餘意想不到的好處。通常來講仍是要從概念開始。不過此次咱們不說概念了,倉庫我的認爲就是放咱們可能須要用到的依賴,插件或者項目輸出的一個存放處。倉庫主要有兩類,本地倉庫和遠程倉庫。還有一種特殊的遠程倉庫就是私服。apache
本地倉庫服務器
在以前例子中,maven學習之三中,配置的就是在本地d盤創建了一個本地庫。一個構件可以被解析使用的前提是必須在本地庫。本地庫的配置方法以下網絡
1: <?xml version="1.0" encoding="UTF-8"?>
2:
3: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"4: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5: xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">6: <!-- localRepository7: | The path to the local repository maven will use to store artifacts.8: |
9: | Default: ~/.m2/repository10: <localRepository>/path/to/local/repo</localRepository>11: -->
12: <localRepository>D:\maven_localRepository</localRepository>
13: ...
14: </settings>
經過設定localRepository標籤來指定本地庫的路徑。
遠程倉庫,本地倉庫一開始是沒有的,它須要從遠處倉庫上下載須要的構件。例如咱們開始的時候須要從中央庫中下載構件。
私服,私服是一個比較獨特的遠程倉庫,他部署在本地局域網,用以免重複下載浪費帶寬。特別是對於網絡環境很差的狀況下,使用私服確實是個不錯的主意。
1: <project xmlns="http://maven.apache.org/POM/4.0.0"2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3: xsi:schemaLocation="http://maven.apache.org/POM/4.0.04: http://maven.apache.org/xsd/maven-4.0.0.xsd">5: ...
6: <repositories>
7: <repository>
8: <releases>
9: <enabled>false</enabled>10: <updatePolicy>always</updatePolicy>
11: <checksumPolicy>warn</checksumPolicy>
12: </releases>
13: <snapshots>
14: <enabled>true</enabled>15: <updatePolicy>never</updatePolicy>
16: <checksumPolicy>fail</checksumPolicy>
17: </snapshots>
18: <id>codehausSnapshots</id>
19: <name>Codehaus Snapshots</name>
20: <url>http://snapshots.maven.codehaus.org/maven2</url>21: <layout>default</layout>22: </repository>
23: </repositories>
24: <pluginRepositories>
25: ...
26: </pluginRepositories>
27: ...
28: </project>
1: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3: xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.04: http://maven.apache.org/xsd/settings-1.0.0.xsd">5: ...
6: <servers>
7: <server>
8: <id>server001</id>
9: <username>my_login</username>
10: <password>my_password</password>
11: <privateKey>${user.home}/.ssh/id_dsa</privateKey>12: <passphrase>some_passphrase</passphrase>
13: <filePermissions>664</filePermissions>
14: <directoryPermissions>775</directoryPermissions>
15: <configuration></configuration>
16: </server>
17: </servers>
18: ...
19: </settings>
id:對應的是pom.xml中repository和mirror中的id,標識須要鏈接的服務器。username,password:對應服務器須要的用戶名和密碼。privateKey,passphrase:對應認真的私鑰文件和字符串。使用私鑰的時候要保證忽略的密碼,由於若是有密碼這個私鑰會被忽略。filePermissions,directoryPermissions:設定文件和目錄的權限。
部署第三方構件是私服的一大特點,你能夠將本身的項目發佈到私服上供其餘團隊成員使用。1: <project xmlns="http://maven.apache.org/POM/4.0.0"2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3: xsi:schemaLocation="http://maven.apache.org/POM/4.0.04: http://maven.apache.org/xsd/maven-4.0.0.xsd">5: ...
6: <distributionManagement>
7: <repository>
8: <uniqueVersion>false</uniqueVersion>9: <id>corp1</id>
10: <name>Corporate Repository</name>
11: <url>scp://repo/maven2</url>12: <layout>default</layout>13: </repository>
14: <snapshotRepository>
15: <uniqueVersion>true</uniqueVersion>16: <id>propSnap</id>
17: <name>Propellors Snapshots</name>
18: <url>sftp://propellers.net/maven</url>19: <layout>legacy</layout>
20: </snapshotRepository>
21: ...
22: </distributionManagement>
23: ...
24: </project>
id,name:用以表示這個遠程倉庫。uniqueVersion:設定爲true的時候表示在發佈時是否生成一個惟一的版本編號或者使用版本編號做爲地址的一部分。url:指定遠程倉庫的路徑和訪問協議。layout:指定倉庫的倉庫的佈局。快照版本:在項目開發過程當中有可能一個模塊依賴於另外一個模塊,可是依賴的模塊處於開發階段,頻繁變動版本,這樣的狀況如何處理比較方便呢,那就是使用快照的方式。例如在發行版2.1上進行開發,那咱們配置爲2.1-SNAPSHOT,如何發佈到私服上,在發佈的過程當中,maven會自動爲版本加上時間戳。另外一個依賴的想項目配置依賴的版本也是2.1-SNAPSHOT,那麼maven會自動尋找最新的版本進行下載更新。鏡像:若是倉庫A可以提供倉庫B全部的文件,那麼咱們就說A是B的鏡像。1: <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3: xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.04: http://maven.apache.org/xsd/settings-1.0.0.xsd">5: ...
6: <mirrors>
7: <mirror>
8: <id>planetmirror.com</id>
9: <name>PlanetMirror Australia</name>
10: <url>http://downloads.planetmirror.com/pub/maven2</url>11: <mirrorOf>central</mirrorOf>
12: </mirror>
13: </mirrors>
14: ...
15: </settings>
id,name:用於描述鏡像。url:鏡像的路徑。mirrorof:central表示中央庫的意思。
1: <settings>
2: ...
3: <mirrors>
4: <mirror>
5: <id>internal-repository</id>
6: <name>Maven Repository Manager running on repo.mycompany.com</name>7: <url>http://repo.mycompany.com/proxy</url>8: <mirrorOf>external:*,!foo</mirrorOf>9: </mirror>
10: <mirror>
11: <id>foo-repository</id>
12: <name>Foo</name>
13: <url>http://repo.mycompany.com/foo</url>14: <mirrorOf>foo</mirrorOf>
15: </mirror>
16: </mirrors>
17: ...
18: </settings>