分發構件至遠程倉庫
mvn install 會將項目生成的構件安裝到本地Maven倉庫,
mvn deploy 用來將項目生成的構件分發到遠程Maven倉庫。本地Maven倉庫的構件只能供當前用戶使用,在分發到遠程Maven倉庫之后,所有能訪問該倉庫的用戶都能使用你的構件。
我們需要配置POM的distributionManagement來指定Maven分發構件的位置,如下:
- <project>
- ...
- <distributionManagement>
- <repository>
- <id>nexus-releases</id>
- <name>Nexus Release Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
- </repository>
- <snapshotRepository>
- <id>nexus-snapshots</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
- ...
- </project>
Maven區別對待release版本的構件和snapshot版本的構件,snapshot為開發過程中的版本,實時,但不穩定,release版本則比較穩定。Maven會根據你項目的版本來判斷將構件分發到哪個倉庫。
一般來說,分發構件到遠程倉庫需要認證,如果你沒有配置任何認證信息,你往往會得到401錯誤。這個時候,如下在settings.xml中配置認證信息:
- <settings>
- ...
- <servers>
- <server>
- <id>nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>nexus-snapshots</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
- ...
- </settings>
需要注意的是,settings.xml中server元素下id的值必須與POM中repository或snapshotRepository下id的值完全一致。將認證信息放到settings下而非POM中,是因為POM往往是它人可見的,而settings.xml是本地的。
小結
本文介紹了Maven倉庫,它是什么?本地倉庫,遠程倉庫,中央倉庫具體是指什么?並介紹了如何在POM中配置項目層次的倉庫,在settings中配置用戶層次的倉庫,以及mirror。本文還介紹了如何安裝構件到本地倉庫,如何分發構件至倉庫。
如果這里不配置,會報錯: 報 錯:Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy (default-deploy) on project git-demo: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
分發構件至遠程倉庫
mvn install 會將項目生成的構件安裝到本地Maven倉庫,
mvn deploy 用來將項目生成的構件分發到遠程Maven倉庫。本地Maven倉庫的構件只能供當前用戶使用,在分發到遠程Maven倉庫之后,所有能訪問該倉庫的用戶都能使用你的構件。
我們需要配置POM的distributionManagement來指定Maven分發構件的位置,如下:
- <project>
- ...
- <distributionManagement>
- <repository>
- <id>nexus-releases</id>
- <name>Nexus Release Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
- </repository>
- <snapshotRepository>
- <id>nexus-snapshots</id>
- <name>Nexus Snapshot Repository</name>
- <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
- ...
- </project>
Maven區別對待release版本的構件和snapshot版本的構件,snapshot為開發過程中的版本,實時,但不穩定,release版本則比較穩定。Maven會根據你項目的版本來判斷將構件分發到哪個倉庫。
一般來說,分發構件到遠程倉庫需要認證,如果你沒有配置任何認證信息,你往往會得到401錯誤。這個時候,如下在settings.xml中配置認證信息:
- <settings>
- ...
- <servers>
- <server>
- <id>nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>nexus-snapshots</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
- ...
- </settings>
需要注意的是,settings.xml中server元素下id的值必須與POM中repository或snapshotRepository下id的值完全一致。將認證信息放到settings下而非POM中,是因為POM往往是它人可見的,而settings.xml是本地的。
小結
本文介紹了Maven倉庫,它是什么?本地倉庫,遠程倉庫,中央倉庫具體是指什么?並介紹了如何在POM中配置項目層次的倉庫,在settings中配置用戶層次的倉庫,以及mirror。本文還介紹了如何安裝構件到本地倉庫,如何分發構件至倉庫。